Smoth LookAt Unity c#
Hello, I made a script that makes the movement of the player follow the rotation of the camera, the problem is that when I rotate my character with each arrow of my keyboard it turns very sharply and even moves a little position, as I could Fix this? I have read about replacing LookAt by Quaternion.Lerp but when I use it it works almost as I would like but the problem is that when I stop pressing an arrow key the character always turns to the same point
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CaminarA : MonoBehaviour { public float horizontalMove; public float verticalMove; private Vector3 playerInput; public CharacterController player; public float playerSpeed; private Vector3 movePlayer; public Camera mainCamera; private Vector3 camForward; private Vector3 camRight; public float velGiro = 12f; private Rigidbody rb; public Transform target;
private Animator anim;
// Start is called before the first frame update
void Start()
{
player = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontalMove = Input.GetAxis("Horizontal");
float verticalMove = Input.GetAxis("Vertical");
playerInput = new Vector3(horizontalMove, 0, verticalMove);
playerInput = Vector3.ClampMagnitude(playerInput, 1);
if (horizontalMove != 0 || verticalMove != 0)
{
anim.SetBool("Caminar", true);
}
else
{
anim.SetBool("Caminar", false);
}
camDirection();
movePlayer = playerInput.x * camRight + playerInput.z * camForward;
player.transform.LookAt(player.transform.position + movePlayer);
player.Move(movePlayer * playerSpeed * Time.deltaTime);
}
void camDirection()
{
camForward = mainCamera.transform.forward;
camRight = mainCamera.transform.right;
camForward.y = 0;
camRight.y = 0;
camForward = camForward.normalized;
camRight = camRight.normalized;
}
}
Your answer
Follow this Question
Related Questions
Look Rotation Viewing Vector is Zero 0 Answers
Grappling hook physics script error 0 Answers
Help with OnTriggerEnter Not working correctly? 1 Answer
Bullet Not moving Forward with speed 1 Answer
Rope & Character controller ? 0 Answers