Hi, I created a third-person controller. I was wondering if anyone could help me make this character turn smoothly.
Here is the code:
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float jumpForce;
[Space(15)]
public float checkDistance;
public Transform groundCheck;
public LayerMask groundMask;
[Space(15)]
public Transform playerMesh;
[Space(15)]
public bool canJump;
public bool canMove;
private void FixedUpdate()
{
Cursor.lockState = CursorLockMode.Locked;
Rigidbody rb = GetComponent<Rigidbody>();
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 forward = Camera.main.transform.forward;
Vector3 right = Camera.main.transform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
Vector3 moveDirection = forward * verticalInput + right * horizontalInput;
rb.velocity = new Vector3(moveDirection.x * speed, rb.velocity.y, moveDirection.z * speed);
if(moveDirection != new Vector3(0, 0, 0))
{
playerMesh.rotation = Quaternion.LookRotation(moveDirection);
}
}
private void Update()
{
canJump = Physics.CheckSphere(groundCheck.position, checkDistance, groundMask);
if (canJump && Input.GetButtonDown("Jump"))
{
Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = Vector3.up * jumpForce;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(groundCheck.transform.position, checkDistance);
}
}
I used quaternion.lookatrotation, but that is super snapy. Can someone teach me how to incoporate Quaternion.Lerp or Slerp?