# 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!

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;
}
``````