x


instantiate finding upstream velocity

How would you go about the following:

I have a rigidbody that is moving at variable speeds. The rigidbody has different children (non-rigidbodies), which in turn have more children, etc, etc,etc. At the end of the chain, One of these children is firing projectiles.

I´m firing these projectiles by instantiating them, and applying an initial velocity. I´ve figured that the way to get these to work properly is to add the velocity of the basic, moving rigidbody to the velocity of the projectile.

However, as I need this script to be generic, I would like to somehow find this velocity without resorting to utilizing gameObject.Find and querying the velocity of the rigidbody. Is there a more efficient way of going about this? Do the children of a rigidbody (even if they are not rigidbodies themselves) inherit the velocity in some query-able way?

Thanks!

more ▼

asked May 21 '11 at 08:24 PM

juanelo gravatar image

juanelo
11 7 7 9

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

1 answer: sort voted first

All child object are even part of the rigidbody. I guess you have no other way than go the hierachy upwards and search your rigidbody. But you only have to do this once at start. You should save the reference in a variable.

JS or C#? it's always the same... (you should be forced to specify your prefered language when posting a question :D)

I guess you use JS?!?

So there are some different ways, but that depends on your setup. Normally rigidbodys should not be a child of something else because that would messup the physics. So to find the root GameObject of your sub-gameobject you can use Transform.root which returns the topmost gameObject in the chain.

var ownerRigidbody : Rigidbody;
function Start() {
    ownerRigidbody = transform.root.rigidbody;
}

function Update() {
    // use the velocity here: ownerRigidbody.velocity
}

If you have your rigidbody as child object (maybe just for grouping) you could search the rigidbody by going the parent chain upwards:

var ownerRigidbody : Rigidbody;
function Start() {
    var current = transform;
    while (current != null && current.rigidbody == null) {
        current = current.parent;
    }
    if (current != null)
        ownerRigidbody = current.rigidbody;
}
more ▼

answered May 21 '11 at 09:12 PM

Bunny83 gravatar image

Bunny83
45.3k 11 49 207

Thank you, Bunny, transform.root was exactly what I needed

May 23 '11 at 11:05 PM juanelo
(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:

x1790
x1672
x318
x254

asked: May 21 '11 at 08:24 PM

Seen: 730 times

Last Updated: May 23 '11 at 11:05 PM