My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.
The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.
How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.
private void Update()
{
HandleInput();
MoveBike();
RotateObject();
}
private void HandleInput()
{
movementDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
movementDirection += Vector3.forward;
}
if (Input.GetKey(KeyCode.S))
{
movementDirection += Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
movementDirection += Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
movementDirection += Vector3.right;
}
movementDirection = movementDirection.normalized;
}
private void MoveBike()
{
transform.position += movementDirection * speed * Time.deltaTime;
}
private void RotateObject()
{
if(movementDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
My player can ride a bike, and the idea is for it to turn to the direction the player is moving rather than snapping into the direction. The player moves with W,A,S,D.
The bike turns, but it's always facing -90 degrees in any direction it is going, so for example, if my player is moving forward/north (camera doesn't turn) the bike is facing left/west.
How can I fix this so if I'm going forward the bike looks forward, and turns smoothly between directions.
private void Update()
{
HandleInput();
MoveBike();
RotateObject();
}
private void HandleInput()
{
movementDirection = Vector3.zero;
if (Input.GetKey(KeyCode.W))
{
movementDirection += Vector3.forward;
}
if (Input.GetKey(KeyCode.S))
{
movementDirection += Vector3.back;
}
if (Input.GetKey(KeyCode.A))
{
movementDirection += Vector3.left;
}
if (Input.GetKey(KeyCode.D))
{
movementDirection += Vector3.right;
}
movementDirection = movementDirection.normalized;
}
private void MoveBike()
{
transform.position += movementDirection * speed * Time.deltaTime;
}
private void RotateObject()
{
if(movementDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(movementDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
}
}
To get angle use Mathf.Atan2
function instead. Here's code to improve rotation of object
private void RotateObject()
{
if (movementDirection != Vector3.zero)
{
//That's for 3D. Use x and y for 2D
float angle = Mathf.Atan2(movementDirection.z, movementDirection.x) * Mathf.Rad2Deg;
//Get quaternion by angle. Rotation by z axis. For other change Vector.forward to another
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//Rotate object
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationSpeed * Time.deltaTime);
}
}
That may not work properly to you because it might rotate by unwanted axis or wrong angle. Edit code values to fit your requirements