[SOLVED] Camera severly jittering
I have a project in which the terrain has lots of trees, lots of detail meshes and post processing applied is on the camera. It is kind of a racing game that I am making. The camera appears to severely jitter on this terrain, however if I import the car prefab, apply the scripts and everything on a completely fresh and plain terrain in another scene, there isn't even a hint of jitter.
Why does this happen and how do I solve it? Please help!
Car Controller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
public Rigidbody sphereRB;
public float speed;
public float rotateForce;
public LayerMask groundLayer;
bool isCarGrounded;
float moveInput;
void Start ()
{
sphereRB.transform.parent = null;
}
void Update ()
{
moveInput = Input.GetAxisRaw("Vertical");
moveInput *= speed;
Debug.Log(moveInput);
transform.position = sphereRB.transform.position;
if (Input.GetAxis("Vertical") != 0 && Input.GetAxis("Horizontal") != 0)
{
foreach( TrailRenderer trail in GetComponentsInChildren<TrailRenderer>())
{
trail.emitting = true;
}
} else
{
foreach (TrailRenderer trail in GetComponentsInChildren<TrailRenderer>())
{
trail.emitting = false;
}
}
}
void FixedUpdate ()
{
RaycastHit hit;
isCarGrounded = Physics.Raycast(transform.position, -transform.up, out hit, groundLayer);
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
if (isCarGrounded)
{
sphereRB.transform.Rotate(0f, rotateForce * Input.GetAxis("Horizontal"), 0f);
transform.Rotate(0f, rotateForce * Input.GetAxis("Horizontal") * Input.GetAxis("Vertical"), 0f);
sphereRB.AddForce(transform.forward * moveInput, ForceMode.Acceleration);
}
}
}
Camera follow script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform camPosTarget;
public Transform camLookTarget;
void FixedUpdate()
{
transform.LookAt(camLookTarget);
transform.position = Vector3.Lerp(transform.position, camPosTarget.position, 10f * Time.deltaTime);
}
}
Issues like this one are often invisible for fps >30, and especially >60. I'm willing to bet money on that the difference between those 2 situations you describe is framerate
First step when fixing anything here is to figure out how to reliably reproduce the problem, so it can be isolated. This simple script will help:
using System.Threading;
using UnityEngine;
public class MainThreadThrottle : MonoBehaviour
{
[SerializeField][Range(0,100)] int _sleepMilliseconds = 0;
void Update () => Thread.Sleep(_sleepMilliseconds);
}
Try throttling your CPU and see if this problem is visible around 30 fps.
@andrew-lukasik My game's running at 50 FPS $$anonymous$$imum. That can't be the problem. The jittering is very noticeable and annoying.
It's not that these physics-rendering synchronization/interpolation issues does not exist at high framerates, it is that those are more apparent under low fps (because delta time is larger, so motion steps as well)
Answer by DarshGupta · Jan 21 at 01:39 PM
I got it! All I had to do was move this line of code -
transform.position = sphereRB.transform.position
and these lines of code -
sphereRB.transform.Rotate(0f, rotateForce * Input.GetAxis("Horizontal"), 0f);
transform.Rotate(0f, rotateForce * Input.GetAxis("Horizontal") * Input.GetAxis("Vertical"), 0f);
in the CarController script to the FixedUpdate()
method.
All the jittering is gone!
However, I don't quite understand why. I know that FixedUpdate runs more frequently than Update, but when I was seeing the car and sphere in the scene view, by putting those lines of code in FixedUpdate, it seemed like the car was trying to catch up with the sphere while the sphere was moving. In Update, it seems like the car is at the same position as the sphere.
Your answer
Follow this Question
Related Questions
Cinematic 2d platformer camera move,2d Camera Movement Player Cinematic 0 Answers
Camera Roll/Headbob? 0 Answers
Camera not following 2D character when the character reaches the ground. 0 Answers
Top-Down Camera view (with rotation) need help (third person script) 0 Answers
CineMachine Camera hangup on respawn 1 Answer