Hello everyone!
I have currently created player movement where you will rotate towards the direction you are moving relative to the current camera (think resident evil). One of the issues I’ve been having is that the character will not stop rotating even when I am not inputing any controls! Here is my code and set-up:
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody _rb;
private Vector3 moveDirection;
[SerializeField] public CinemachineVirtualCamera currentCamera;
[SerializeField] private float _movementSpeed;
[SerializeField] private float _turnSpeed;
// Start is called before the first frame update
void Start()
{
_rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
_rb.velocity = _movementSpeed * Time.deltaTime * moveDirection;
if (moveDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
_rb.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, _turnSpeed * Time.deltaTime);
}
}
void OnMove(InputValue inputValue)
{
moveDirection = inputValue.Get<Vector2>();
moveDirection = new Vector3(moveDirection.x, 0 ,moveDirection.y);
moveDirection = ChangeMoveDirectionRelativeToCamera();
//transform.Translate(moveDirection * _movementSpeed * Time.deltaTime );
}
Vector3 ChangeMoveDirectionRelativeToCamera()
{
var camForward = currentCamera.transform.forward;
var camRight = currentCamera.transform.right;
camForward.y = 0;
camRight.y = 0;
camForward.Normalize();
camRight.Normalize();
var desiredMoveDirection = camForward * moveDirection.z + camRight * moveDirection.x ;
return desiredMoveDirection;
}
}
And here are my input actions and RigidBody settings: