- Home /
Can an instantiated object inherit its parent's velocity?
I have an object which destroys itself then creates a new object at the same time and it works fine. The rotation and everything is all the same but the velocity resets which doesn't really make it seem that seamless. So is there a way i can get the instantiated object to continue with its parent's velocity?
Answer by duck · Feb 14, 2010 at 02:00 PM
You can set the object's velocity immediately after instantiating it.
In general, you shouldn't use the ability to set velocity directly as a method of controlling or moving the object (you should apply forces instead), but in this situation where you are setting its initial velocity after creating it, it should be fine:
// create the new object var newObject = Instantiate( sourceObject, position, rotation );
// give it the same velocity as the current object newObject.rigidbody.velocity = rigidbody.velocity; newObject.angularVelocity = rigidbody.angularVelocity;
// destroy this object Destroy(gameObject);
You might also find that you need to set up an IgnoreCollision between the old and new objects - if they both have colliders, there's a chance a physics update may occur while they both exist in the same position.
You're welcome - please upvote the answer and click the 'accept' tick to indicate the question has been answered :) Duck 1 $$anonymous$$ ago
I have a question.
In my case, I have a non-rigidbody weapon object as a child of my rigidbody player, and my rockets spawn from the weapon. So your solution does not work in its current form.
How do I find the velocity of the root object in a hierarchy?
simply have a variable, boss, that points to the rigidbody
just set it in the editor (ie, drag it)
{ if you don't want to set it in the editor, just use ".parent" to find it in code.
if (for some reason) you truly want to generally find the highest object, search on here for 1000s of questions about that, or ask a new separate question }
then use boss.velocity
Answer by Atticus052 · Feb 04, 2021 at 03:48 PM
{
//Get parent ship velocity
Vector3 shipVelocity = PlayerShip.GetComponent<Rigidbody2D>().velocity;
//spawn projectile
GameObject shot = Instantiate(ammoType, firePoint.position, firePoint.rotation);
//create vector3 to store ship velocity
Vector3 velocity = new Vector3(shipVelocity.x, shipVelocity.y, shipVelocity.z);
//get RB component of bullet
rb = shot.GetComponent<Rigidbody2D>();
rb.velocity = velocity;
//if you want to add force to it, and have it maintain rotation of your parent object
rb.AddForce(firePoint.up* bulletSpeed, ForceMode2D.Impulse);
Destroy(shot, bulletLifespan);
}
Your answer
Follow this Question
Related Questions
How to Instantiate prefab as child? (Java) 1 Answer
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer
How to Instantiate and change velocity 2 Answers
Trying to instantiate prefabs to a parent object with C# script. 1 Answer
How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse? 2 Answers