x


Rotating an object to match a rigidbody's direction of movement.

I have a parent object which is a capsule collider with rigidbody. The game I'm making is 2D physics in an underwater environment, so the object has a configurable joint locking all axes except X and Y movement.

When the player presses the WASD keys or uses an XBox 360 controller to give movement input the appropriate force is applied to the capsule's rigidbody, pushing it through the water in that direction.

I need to be able to rotate a child of this object so that it looks like it is swimming in that direction. I can't match the parent's angle because it is locked, I need to match its direction of movement with Z rotation.

more ▼

asked Jan 01 '11 at 04:33 PM

CrowbarSka gravatar image

CrowbarSka
45 3 4 8

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

1 answer: sort newest

I just had to do a similar thing with a tank firing a projectile and I wanted the projectile to "Look" in its direction of movement. The code I used is the following called in Update() in a script attached to the projectile.

transform.rotation = Quaternion.LookRotation(rigidbody.velocity);

I had some problems at first with an odd twisting motion which was due to some non-uniform scaling on the object. If you have such scaling I would suggest parenting it to a non scaled empty gameobject.

more ▼

answered Jan 01 '11 at 05:08 PM

DanMarionette gravatar image

DanMarionette
177 11 12 21

Just what I needed, much appreciated!

Jan 01 '11 at 05:44 PM CrowbarSka

Thanks a lot. Works like a charm.

Dec 31 '11 at 05:49 PM bradmarxmoosepi
(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:

x2174
x1882
x1041

asked: Jan 01 '11 at 04:33 PM

Seen: 2553 times

Last Updated: Dec 31 '11 at 05:49 PM