- Home /
How to rotate object in direction of movement without drift?
I have a semi-top down game that moves with horizontal and vertical input. I do NOT want to rotate the player based on the mouse position like most top down shooters, so rotating based on movement direction is preferred. Everything works fine, however most of the time if both inputs are pressed at the same time and then let go simultaneously, the player will slightly rotate a little extra similar to a drift. It's not significant, but noticeable. I'm not sure where to try and fix this problem. Is it possible that the input settings are too sensitive? I have tinkered with different options with no change, any suggestions would be great.
public float speed = 4f; public float rotateSpeed = 6f;
private bool canMove = true;
private Vector3 movement;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (!canMove)
return;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Move(h, v);
Rotate(h, v);
}
void Move(float h, float v)
{
Vector3 movement = new Vector3(h, 0, v) * speed * Time.deltaTime;
rb.MovePosition(transform.position + movement);
}
void Rotate(float h, float v)
{
Vector3 desiredDirection = Vector3.Normalize(new Vector3(h, 0f, v));
if (desiredDirection != Vector3.zero)
{
Quaternion rotation = Quaternion.LookRotation(desiredDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotateSpeed * Time.deltaTime);
}
}
Answer by splitter20 · Nov 14, 2017 at 07:08 AM
Just needed to check for input first . . .
if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
Rotate(h, v);
}
Your answer
Follow this Question
Related Questions
Rotating about and only one axis 1 Answer
Slerp to make the right/left side face another object 2 Answers
Basic AI Locked Axis 1 Answer
Third person Character Controller slowing down when camera is pointing towards the ground 2 Answers
Problem with turning tower in players direction (different angle and shaking) 1 Answer