Hello ! I am in an unity fps project so I create a script to move my player and to move the camera but the problem is when I move my player and the camera at the same time looking at an object. The object is jerky … (https://youtu.be/qQ1G3LH0aS4)
**
PlayerController :
**
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
public LayerMask layerMask;
public bool Grounded;
public float speed = 6;
public float speedBoost = 5;
public float JumpForce = 4;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
//Input
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
//Moving
Vector3 direction = new Vector3(x, 0, z);
rb.MovePosition(rb.position + transform.TransformDirection(direction) * speed * Time.fixedDeltaTime);
if (direction.sqrMagnitude > 1)
direction.Normalize();
//transform.Translate(direction * speed * Time.fixedDeltaTime);
//rb.AddForce(direction * speed * Time.fixedDeltaTime);
//Grounding
Grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
//Jumping
if (Input.GetKeyDown(KeyCode.Space) && Grounded)
{
rb.velocity = new Vector3(rb.velocity.x, JumpForce, rb.velocity.z);
}
//Sprinting
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed += speedBoost;
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed -= speedBoost;
}
}
}
**
PlayerLook :
**
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
public Transform player;
public float mouseSensitivity = 4;
private float x = 0;
private float y = 0;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
//Input
x += -Input.GetAxis("Mouse Y") * mouseSensitivity;
y += Input.GetAxis("Mouse X") * mouseSensitivity;
//Clamping
x = Mathf.Clamp(x, -90, 90);
//Rotation
transform.localRotation = Quaternion.Euler(x, 0, 0);
player.transform.localRotation = Quaternion.Euler(0, y, 0);
}
}