Hello,
I got a problem with smooth player rotating. When i use opposite directions buttons like W - S or WD - AS or A - D everything works fine but when i want to rotate on W to D or W to WD it’s weird - it’s going so slow.
Rotation_1:
Quaternion_Rotate_From = transform.rotation;
Quaternion_Rotate_To = Quaternion.Euler (0,targetAngle,0);
transform.rotation = Quaternion.Slerp (Quaternion_Rotate_From, Quaternion_Rotate_To, Rotation_Smoothness);
Rotation_2:
transform.rotation = Quaternion.Euler(0f, targetAngle,0f); // Without Lerp instant rotation
Even on that instant rotation from Rotation_2 when i want to rotate from W to WD it’s going very slow.What i want to achieve is smooth rotation in every direction and always in same amount of time like .2f;
Thanks for help!
Full code:
float horizontal;
float vertical;
Vector3 desiredMoveDirection;
public Transform cam;
private CharacterController controller;
float terrainHeight;
public float moveSpeed = 6f;
float targetAngle;
public float Rotation_Smoothness_Time = 0.2f;
[SerializeField] private float Rotation_Smoothness;
private Quaternion Quaternion_Rotate_From;
private Quaternion Quaternion_Rotate_To;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
PlayerMoveAndRotation();
TerrainGravity();
}
void PlayerMoveAndRotation()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical);
targetAngle = Mathf.Atan2(direction.x, direction.z) *Mathf.Rad2Deg + cam.eulerAngles.y;
desiredMoveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
if(horizontal != 0 || vertical != 0)
{
if(Rotation_Smoothness >= Rotation_Smoothness_Time)
{
Rotation_Smoothness = 0f;
}
else
{
Rotation_Smoothness += Time.deltaTime;
}
Quaternion_Rotate_From = transform.rotation;
Quaternion_Rotate_To = Quaternion.Euler (0,targetAngle,0);
transform.rotation = Quaternion.Slerp (Quaternion_Rotate_From, Quaternion_Rotate_To, Rotation_Smoothness);
//transform.rotation = Quaternion.Euler(0f, targetAngle,0f); // Without Lerp instant rotation
controller.Move(desiredMoveDirection.normalized * moveSpeed * Time.deltaTime);
if(horizontal != 0)
{
cam.transform.Rotate(new Vector3(0, 1, 0), horizontal/ 4f, Space.World);
}
}
else
{
controller.Move(new Vector3(0f, desiredMoveDirection.y, 0f).normalized * moveSpeed * Time.deltaTime);
}
}