- Home /
Question by
Morsology · May 25, 2021 at 02:08 AM ·
rotationrigidbodyrotate objectrigidbody physicsrotations
How to make a cube turn in the direction it is moving?
So I have a red cube on a plane. The cube has a rigidbody and is using force to move. I need it to rotate in the direction it is moving but I am not sure how to. All the solutions I have found apply to things moving with Vector3 (I am not sure if that matters but none of them worked for me). So is there a simple way to make the cube face in the direction it is moving?
private Rigidbody _rigidbody;
[SerializeField] private float _movementForce = 10f;
private void Awake() => _rigidbody = GetComponent<Rigidbody>();
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{
_rigidbody.AddForce(_movementForce * transform.forward);
}
if (Input.GetKey(KeyCode.S))
{
_rigidbody.AddForce(_movementForce * -transform.forward);
}
if (Input.GetKey(KeyCode.D))
{
_rigidbody.AddForce(_movementForce * transform.right);
}
if (Input.GetKey(KeyCode.A))
{
_rigidbody.AddForce(_movementForce * -transform.right);
}
}
image.png
(16.2 kB)
Comment
Answer by sansuno68 · May 26, 2021 at 10:10 AM
Hope the following Post is able to help u :D https://answers.unity.com/questions/1701308/rotate-object-in-direction-of-movement.html