- Home /
My player's camera displays jerky objects
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);
}
}
Answer by Nistroy · Jul 04, 2021 at 09:33 AM
Try this code @ZeyFight , I just changed to MoveTowards to make the rotation smoother.
public Transform player;
public float lookSpeed = 10f;
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(Vector3.MoveTowards(transform.localRotation.eulerAngles, new Vector3(x, 0, 0), lookSpeed * Time.deltaTime));
player.transform.localRotation = Quaternion.Euler(0, y, 0);
}
Hello ! Thank you for your reply ! I tried your script but the problem is still present ... Your script changed my vertical axis, I had trouble moving up and down and when I moved up, the camera kept going up without me even touch my mouse
Afterwards I don't know if you know it, but when you play your game on unity it's normal that your game works less well than when you build it. It is to improve performance that it sometimes gets weird. Sorry if my code doesn't work completely, I think the other user's response should work better!
Answer by andrew-lukasik · Jul 04, 2021 at 12:30 PM
Makes mouse look as smooth as silk.
note: this code won't help with ugly jitter coming from controller movement. It is there 100% because a camera being parented to a
Rigidbody
is sure way to produce it. And the lower the framerate the more visible it becomes.
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[SerializeField] Transform _player;
[SerializeField] float _mouseSensitivity = 100f;
[SerializeField][Min(1f/50f)] float _mouseInterpolationTime = 0.1f;
( float horizontal , float vertical ) _mouse, _mouseDir, _mouseInterpolated, _angle;
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
_mouse = ( horizontal: Input.GetAxisRaw("Mouse X") , vertical: Input.GetAxisRaw("Mouse Y") );
if( _mouse.horizontal<0 ) _mouseDir.horizontal = -1;
if( _mouse.horizontal>0 ) _mouseDir.horizontal = 1;
if( _mouse.vertical<0 ) _mouseDir.vertical = -1;
if( _mouse.vertical>0 ) _mouseDir.vertical = 1;
// note: don't replace this with Mathf.Sign as 0 must stay ignored
}
void FixedUpdate ()
{
float deltaTime = Time.fixedDeltaTime;
// look left-right
if( Mathf.Sign(_mouseInterpolated.horizontal)==_mouseDir.horizontal )
{
_mouseInterpolated.horizontal = Mathf.Lerp(
_mouseInterpolated.horizontal ,
_mouse.horizontal * _mouseSensitivity ,
(1f/_mouseInterpolationTime) * deltaTime
);
}
else
{
_mouseInterpolated.horizontal = _mouse.horizontal;
}
_angle.horizontal += _mouseInterpolated.horizontal * deltaTime;
_player.localRotation = Quaternion.Euler( 0 , _angle.horizontal , 0 );
// look up-down
if( Mathf.Sign(_mouseInterpolated.vertical)==_mouseDir.vertical )
{
_mouseInterpolated.vertical = Mathf.Lerp(
_mouseInterpolated.vertical ,
_mouse.vertical * _mouseSensitivity ,
(1f/_mouseInterpolationTime) * deltaTime
);
}
else
{
_mouseInterpolated.vertical = _mouse.vertical;
}
_angle.vertical = Mathf.Clamp( _angle.vertical - _mouseInterpolated.vertical * deltaTime , -90f , 90f );
transform.localRotation = Quaternion.Euler( _angle.vertical , 0 , 0 );
}
}
Hello ! Thank you for your reply ! it seems to work but I feel a force when I move my mouse ... I try to modify the sensitivity, it does not change anything ... What to do?
I updated the code. Change in mouse look direction overwrites interpolation.
Your answer
Follow this Question
Related Questions
Best way for rigidbody enemy? 1 Answer
FPS recoil 1 Answer
Problem with multiplayer unity. 1 Answer
Low framerate on physics initialization. 0 Answers