Answer the question
In order to leave comments, you need to log in
(Unity 3D, character movement) Why is running diagonally slower than running right/left or straight/backward?
Hello. I made the movement of the character in 3d, but it turns out that when the character moves diagonally, and not in a specific direction, then his speed becomes less. I did not find a similar problem on the Internet, I do not understand what's wrong.
I make for mobile, so I use the Input System for the joystick.
private void Start()
{
controller = GetComponent<CharacterController>();
cameraMain = Camera.main.transform;
anim = GetComponent<Animator>();
animAimRun = false;
}
void Update()
{
Vector2 movementInput = playerInput.PlayerMain.Move.ReadValue<Vector2>(); // движение джойстика
Vector3 move = (cameraMain.forward * movementInput.y + cameraMain.right * movementInput.x);
controller.Move(move * Time.deltaTime * playerSpeed); // x z
anim.SetFloat("inputX", movementInput.x);
anim.SetFloat("inputY", movementInput.y);
if (movementInput != Vector2.zero) // rotation
{
float targetAngle = Mathf.Atan2(move.x, move.z) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question