- Home /
How to Instantiate and change velocity
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...
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:
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);
}
}
Answer by sparkzbarca · Aug 06, 2013 at 03:39 AM
first both those have + velocity so both are faster than the first.
Second and this is probably a big issue
they ignore the original collider but the clones can still collide with each other.
you need to ignorecollision clone1, clone2
Third get rid of the clone control function altogether. just do it inside creation also the current velocity to figure out what to add and subtract lastly use addforce not velocity directly.
clone = instantiate (coconut, position,rotation);
clone.rigidbody.addforce(-rigidbody.velocity.normalized * speed)
clone2 = .....
clone2.rigidbody.addforce (rigidbody.velocity.normalized * speed)
notice one has - in front and one doesnt. speed will be the difference in how much the front is forward and the back is back.
Answer by samz · Aug 06, 2013 at 07:52 AM
THANK YOU SO MUCH!!!!!!!!!!
rigidbody.AddForce(rigidbody.velocity.normalized);
rigidbody.velocity = Vector3.right * speed;
This has helped me loads, i added number2 since number1 would always give me a velocity of (0,0,0). And i need to keep the CloneControl otherwise the clones would incorporate the same velocity change as the original coconut being thrown, making it jump in mid-air after being instantiated.
but you deco put me on the right track!!!! thank you once again
oh yes im sorry 1 would initially thats because if you have a starting velocity of zero the normal of that is zero and of course zero multiplied by anythign is zero.
what you could do is copy the velocity of the base
clone1.rigidbody.velocity = parent.rigibody.velocity;
then
addforce(velocity.normalized * speed)
taht will work and that will include your movement in all 3 directions down and forward and depth and scuh