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.
asked Jan 01 '11 at 04:33 PM
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.
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.
answered Jan 01 '11 at 05:08 PM