- Home /
Instantiation with inheriting velocity problem
while (count < numberofBouncyBalls) { var eplosive1 : Rigidbody = Instantiate(bouncyExplosive, transform.position, transform.rotation);
eplosive1.rigidbody.velocity = rigidbody.velocity;
eplosive1.angularVelocity = rigidbody.angularVelocity;
count = count + 1;
}
Alright I am attempting to use this for something I am making where a single rigidbody bouncy ball splits into 3 or as many as I want other bouncy balls, and it is easy to simply instantiate an amount of them however they simply start bouncing up and down boaringly, I am trying to use this to give each ball the velocity and such of the ball they are technically separating from but for some reason I get this error.
InvalidCastException: Cannot cast from source type to destination type. Separate+Start$20+$.MoveNext () (at Assets\WeaponScripts\Separate.js:20)
I am not to experienced with javascript so I was wondering if anyone could point me towards the reason why this is happening or a fix to my problem, cheers.
Top of your code sample was clipped off in the format, don't forget checking the post previewer when posting , yes I'm nitpicky , but it helps with the Q&A process
Answer by jameslee0227 · Nov 18, 2010 at 08:11 AM
recommand
more better fucntion
rigidbody.AddForce(velocity, ForceMode.VelocityChange);
rigidbody.AddTorque(angularVelocity, ForceMode.VelocityChange);
This isn't needed. There's a reason the velocity and angularVelocity properties are read/write. Original code fine.
Answer by Jonathan Czeck · Jan 20, 2011 at 12:48 AM
Probably your bouncyExplosive prefab does not have a Rigidbody component attached to it, so when you cast it to Rigidbody and assign to variable eplosive1, it spits out that error.
Your answer