# Implementing RPG WASD 3rd person movement and getting a weird drift

``````public void WasdMove()
{
if (gameObject.tag == "Player")
{
float turnSmoothTime = 0.05f;
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Math.Sign(Input.GetAxis("Vertical"));
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

if (direction.magnitude >= .1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + mainCam.eulerAngles.y;
print(targetAngle);
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);

Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
navMeshAgent.Move(moveDirection.normalized * maxSpeed * Time.deltaTime);
navMeshAgent.speed = maxSpeed;
navMeshAgent.velocity = direction;
GetComponent<ActionScheduler>().CancelCurrentAction();
}
}
}
``````

Here’s a quick clip showing the drift I’m trying to eliminate. I have a freelook cinemachine camera doing the follow camera effect that moves around when holding right click.

I’m getting this weird drift effect and I don’t know how to eliminate it. It seems like it’s some how attached to the direction the camera is facing. I noticed that it drifts to slightly different angles depending on where the camera is facing as I rotate around the player. But if I hold ‘W’ the character doesn’t go exactly straight. I doesn’t seem to happen when moving from left to right, however. Only when going up or down on the vertical input.

Thoughts?

Fwiw the math.sign is only there cause I was trying to make the input.vertical a whole number to see if that had anything to do with it.

Ohhhhh I kinda figured it out. I was trying to set the navMeshAgent speed and velocity again at the bottom of the function so I could use those values to set my animator. But essentially that was adding an additional weird value to the movement and creating the drift effect

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.