- Home /
How do I stop my player or camera from jittering during player movement?
In my game, I have a third person character. The player moves with a rigidbody and has isKinematic set off with interpolate on. In the script, it moves with Rigidbody.MovePosition with the y position froze. Here's my player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody rb;
public Transform camPos;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
camPos = Camera.main.transform;
}
private void FixedUpdate()
{
if (Input.GetKey("w"))
{
rb.MovePosition(transform.position + (camPos.forward * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("s"))
{
rb.MovePosition(transform.position + (-camPos.forward * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("d"))
{
rb.MovePosition(transform.position + (camPos.right * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("a"))
{
rb.MovePosition(transform.position + (-camPos.right * speed * Time.fixedDeltaTime));
}
rb.velocity = Vector3.zero;
}
}
For my camera, I have it lookat the player and lag behind smoothly. Here's that script:
//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamPos : MonoBehaviour
{
public Transform target;
public float smoothTime = 0.5F;
private Vector3 velocity = Vector3.zero;
public GameObject player;
private float mouseX, mouseY;
public int mouseX_Speed, mouseY_Speed;
//mouseSensitivity
public int turnSpeed;
void getMouseXY()
{
mouseX += Input.GetAxis("Mouse X") * smoothTime;
mouseY -= Input.GetAxis("Mouse Y") * smoothTime;
}
void LateUpdate()
{
getMouseXY();
// Define a target position above and behind the target transform
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 1.5f, -2.5f));
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
transform.LookAt(target);
target.rotation = Quaternion.Euler((mouseY * mouseY_Speed), (mouseX * mouseX_Speed), 0);
player.transform.rotation = Quaternion.Euler(0, (mouseX * turnSpeed), 0);
}
}
My issue is that whenever the player moves either the camera/player jitters. I have debugged the player transform y position to make sure it wasn't moving up or down(it doesn't). To help with this issue I have either turned down the Fixed Timestep under Time in the project settings or put the code in the camera script is fixed update instead of late update(which I have researched is an issue). I know MovePosition for the player isn't the right way to move, but I don't know how to make the player move in the direction the camera is facing with rb.velocity. I suspect the issue may lay in the player script with variable camPos. Either way, I would like to stop the jittering.
Does it improve if you make your camera follow script run in FixedUpdate(), rather than LateUpdate()?
Then the jittering is caused by the fact that LateUpdate() gets called at a different frame rate to FixedUpdate(). This beaks the sync between the camera move and the player move.
Answer by DoubleDotDee · Jul 30, 2020 at 06:37 PM
@nexteon1 My game was 2D and the script was very different but i fixed it by increasing or decreasing the speed of the camera.
Your answer
Follow this Question
Related Questions
Rigidbody movement in direction of camera? 1 Answer
How to stop input after hitting a certain velocity 1 Answer
Camera shaking/jittering? 2 Answers
RB Interpolation 1 Answer