- Home /
How can I rotate a rigid body based on its velocity?
Hi,
I have a tank that can fire a projectile(rigidbody) at an angle and force specified by the player. A pretty basic artillery game mechanic. The projectile fires in the correct direction but at the moment, left to the physics engine, the projectile(which is a capsule at the moment) doesn't rotate or "point" in the direction of travel as it moves through the air.
Does anyone know how I can achieve this effect?
Thanks
UPDATED 27/12/10
TankControl.cs (attached to the tank - projectile instantiation Method) shellSpawn is an empty gameobject that is a child of the tank gun barrel to spawn the projectile from.
void Fire() { //set the position and rotation of the projectile the the shell spawn point Vector3 position = shellSpawn.position; Quaternion rotation = shellSpawn.rotation;
GameObject firedProjectile = (GameObject)Instantiate(shell, shellSpawn.position, shellSpawn.rotation);
firedProjectile.rigidbody.AddRelativeForce(new Vector3(0,1,0) * gunPower, ForceMode.Impulse);
}
Projectile.cs (attached to projectile prefab - Update Method)
void Update () {
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
Worked in Unity 5.6. below is my class. I just added the new script to the Shell prefab and applied.
using UnityEngine;
public class ShellDirection : $$anonymous$$onoBehaviour
{
// Update is called once per frame
void Update()
{
Rigidbody shellRigidbody = gameObject.GetComponent<Rigidbody>();
gameObject.transform.rotation = Quaternion.LookRotation(shellRigidbody.velocity);
}
}
Answer by Mickydtron · Dec 20, 2010 at 10:33 PM
transform.rotation=Quaternion.SetLookRotation(velocity);
This will set your rotation in the exact same direction that you are moving in. This assumes that you have a Vector3 velocity which you are using. It seems a fair assumption, all things considered.
Thanks for the reply, sorry it's been a while getting back to you but Christmas has taken over for the last week.
I have added what you have suggested to a script attached to the projectile prefab and it does look like its "trying" to do what I want but for my setup it isn't quite working. The projectile twists in the air. It's like it's rotating on two axis not just the one it should.
I have added the code to my question.
I'm not sure what you're saying it's doing. so it currently points its nose in the direction of travel, but also spins around its own z axis? Is there any other code that could be affecting the rotation?
I fixed the problem. It was due to non-uniform scaling on the prefab. With your answer it works perfectly now. Thanks.
Quaternion.SetLookRotation() is now a void method and works with this syntax:
transform.rotation.SetLookRotation(velocity);
This answer is great, but I have an extra question.
I want to
transform.Rotate(-Input.acceleration.z, 0, 0);
AND Rotate towards rigid.velocity
transform.rotation = Quaternion.LookRotation(rigid.velocity, Vector3.up);
How can I do both?
Answer by Bryan-Legend · May 01, 2015 at 06:35 PM
Here's the code that worked for me:
transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
Answer by Elson · Aug 18, 2014 at 07:32 PM
//rotates rigidbody to face its current velocity public void RotateToVelocity(float turnSpeed, bool ignoreY) {
Vector3 dir; if(ignoreY) dir = new Vector3(rigidbody.velocity.x, 0f, rigidbody.velocity.z); else dir = rigidbody.velocity;
if (dir.magnitude > 0.1)
{
Quaternion dirQ = Quaternion.LookRotation (dir);
Quaternion slerp = Quaternion.Slerp (transform.rotation, dirQ, dir.magnitude * turnSpeed * Time.deltaTime);
rigidbody.MoveRotation(slerp);
}
}