x


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?

more ▼

asked Feb 14 '10 at 01:23 PM

Evil Tebor gravatar image

Evil Tebor
63 6 7 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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.

more ▼

answered Feb 14 '10 at 02:00 PM

duck gravatar image

duck ♦♦
41.4k 95 152 415

Worked brilliantly thanks. :D

Feb 14 '10 at 02:08 PM Evil Tebor

You're welcome - please upvote the answer and click the 'accept' tick to indicate the question has been answered :) Duck 1 min ago

Feb 14 '10 at 04:19 PM duck ♦♦

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?

Sep 27 '12 at 08:07 AM Dubious Drewski

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

Sep 27 '12 at 08:14 AM Fattie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1726
x440
x330

asked: Feb 14 '10 at 01:23 PM

Seen: 2232 times

Last Updated: Sep 27 '12 at 08:14 AM