Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

How to Instantiate and change velocity

$
0
0
I have a coconut which my player throws, where in mid-air it will multiply into 3 coconuts. 1 of which will have less velocity than the original and the other will have more, like this... ![alt text][1] now the problem is that the velocity from my code works whenever it wants. So sometimes these 3 coconuts are very close together and do not separate, like this: ![alt text][2] i really dont understand why this happens, but playing around with my code i noticed that if i don't destroy these 2 additional clones then it works fine. Heres my code: This script is used by both the original coconut and the clones, the variable "clonesMade" distinguishes between the two function Start() { Physics.IgnoreCollision(MyStaticVar.playerTransform.FindChild("Collider").collider, collider); if( !MyStaticVar.clonesMade ) { // Wait until throwing animation is complete before executing the launch yield WaitForSeconds (MyStaticVar.playerTransform.animation["Throw"].length-0.7); ThrowMulti(); } else { CloneControl(); } } //******************************************************************************************** function ThrowCoconut() { // Add launch force inrelation to the direction the player is facing transform.rigidbody.velocity = Vector3(35,50,0); } //******************************************************************************************** function ThrowMulti() { ThrowCoconut(); CreateClone(); } //******************************************************************************************** function CreateClone() { MyStaticVar.clonesMade = true; yield WaitForSeconds(0.5); // Multi-coconut 1 var clone1 : Transform = Instantiate(transform, transform.position, transform.rotation); clone1.name = "Clone1"; Physics.IgnoreCollision(clone1.collider, collider); // Multi-coconut 2 var clone2 : Transform = Instantiate(transform, transform.position, transform.rotation); clone2.name = "Clone2"; Physics.IgnoreCollision(clone2.collider, collider); } // ********************************************************************************************* function CloneControl() { if( name=="Clone1" ) { transform.rigidbody.velocity += Vector3(5,0,0); } else if( name=="Clone2" ) { transform.rigidbody.velocity += Vector3(50,0,0); MyStaticVar.clonesMade = false; } } //******************************************************************************************** function OnCollisionEnter (col : Collision) { // slow down when on platform if( col.transform.tag=="Platform" ) { rigidbody.velocity.x = 2; } // Destroy if( name=="Pfb_Throw_PowerUp(Clone)" ) { Destroy(gameObject, 2); } else if(name=="Clone1" || name=="Clone2") { Destroy(gameObject, 2.5); } } [1]: /storage/temp/13941-_good.png [2]: /storage/temp/13942-_bad.png

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>