Get the velocity of parent object

I am trying to rotate an object (should NOT be rigidbody) based on the velocity of its parent (rigidbody); It must be awfully simple yet it eludes me; just learning my way through js and can't get to the end of it; any advice/docs links - I appreciate. Thanks!

2 Answers

2

Do you mean the linear or angular velocity of the parent, and how should they relate?

As an example, the faster this thing goes in any direction, the faster the child will spin on its Y axis:

(script on child, untested)

Update()
{
  transform.Rotate(0, transform.parent.GetComponent(Rigidbody).velocity.magnitude, 0);
}

and you'd want to scale that or it would spin very very fast, but it depends on how your objects actually move.

This should work, its the behavior I am looking for. The faster the parent goes, the faster the child spins, once the parent velocity drops to smaller values, the spin should match as well, slowing down accordingly. Thanks!

..how do I declare the rigidbody in this context (unknown identifier)? :P

Sorry should be Rigidbody

not really an answer since it doesn’t work properly just yet. As the parent object gains speed the child objects rotates as it should - but as soon as I stop, the child object halts although the parent objects still has some velocity.

here’s the code I use. Also, I should probably use a local velocity and avoid using the GetAxis little hack. Any ideas? Much appreciated!

var parentObject : GameObject;

function Update(){
	var speed = parentObject.GetComponent(Rigidbody).velocity.magnitude* 0.5f;
	
	var z=Input.GetAxis("Vertical")*5 *speed;

	var childObjects = GameObject.FindGameObjectsWithTag("stuff");
    for (var childObject in childObjects)
    childObject.transform.Rotate(0,0,-z);
}