The question is answered, right answer was accepted
Why my camera is jittering?(Wheel colliders)
I have a big problem with camera follow , I tried all methods what I found on the internet in last 3 days , absolutely everything , but the problem is still the same.
(The camera is tracking the car)
Camera is jittering and makes small shakes.
Methods:
Change rigidbody interpolate Change Updates from camera and car controller Making another scripts Changing colliders Add Vector3.Lerp or Vector3.SmoothDamp Changing wheelcolliders parameters Reinstall unity and we are still there.
Do you have any idea what's going on?
Answer by streeetwalker · Dec 12, 2020 at 07:53 PM
Yes, wheel colliders are finicky! Their springs oscillate even when it appears the car is at rest.
If you have parented the camera to the car that can be major problem. Your best bet is to not directly parent the camera.
If you are not, you can also set up your code so the camera does not move unless the motion of the car crosses some threshold, and also ignore the y axis if you do not have any vertical height on your track.
Here is a camera script I created I have had good luck with - maybe you can too, and modify it to your needs. You can use it with a fixed offset or control it using the mouse - just choose the options in the inspector. Set up the initial camera position relative to the car in the scene before you run the game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// streetwalkers's camera follower modification
public class CameraFollower : MonoBehaviour {
[Header( "Target Parameters" )]
public Transform target;
public Vector3 offset;
public float followSpeed = 2;
public float lookSpeed = 3;
public bool fixedCamera;
public bool relativeCamera;
private float distance;
[Header( "Camera Rotation Speeds" )]
public float y_sensitivity = 5f;
public float x_sensitivity = 5f;
// Other private variables
private float pitch, yaw;
private bool dragging = false;
Vector3 c1Pos;
Quaternion c1Rot;
private void Start() {
offset = transform.position - target.position;
c1Pos = target.parent.position;
c1Rot = target.localRotation;
}
void Update() {
if( Input.GetMouseButtonDown( 2 ) ) {
target.parent.position = c1Pos;
target.localRotation = c1Rot;
target.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
transform.position = target.position + offset;
if( Input.GetMouseButtonDown( 0 ) ) {
dragging = true;
}
if( Input.GetMouseButtonUp( 0 ) ) {
dragging = false;
}
pitch = 0;
yaw = 0;
if( Input.GetMouseButton( 0 ) && dragging ) {
pitch = -Input.GetAxis( "Mouse Y" ) * y_sensitivity;
yaw = Input.GetAxis( "Mouse X" ) * x_sensitivity;
}
float wheelDelta = Input.GetAxis( "Mouse ScrollWheel" );
Zoom( wheelDelta );
RotateAround();
}
private void FixedUpdate() {
if( !relativeCamera ) {
LookAtTarget();
MoveToTarget();
}
}
private void LookAtTarget() {
Vector3 _lookDirection = target.position - transform.position;
Quaternion _rot = Quaternion.LookRotation( _lookDirection, Vector3.up );
transform.rotation = Quaternion.Lerp( transform.rotation, _rot, lookSpeed * Time.deltaTime );
}
private void MoveToTarget() {
if( fixedCamera ) {
transform.position = offset;
} else {
Vector3 _targetPos = new Vector3( target.position.x, 0, target.position.z ) +
target.forward * offset.z +
target.right * offset.x +
Vector3.up * offset.y;
transform.position = Vector3.Lerp( transform.position, _targetPos, followSpeed * Time.deltaTime );
}
}
private void Zoom( float wheelDelta ) {
if( Mathf.Abs( wheelDelta ) > 0 ) {
Vector3 direction = offset;
distance = direction.magnitude;
direction = direction.normalized;
distance = distance - wheelDelta;
offset = direction * distance;
}
}
private bool RotateAround() {
bool rotated = false;
if( Mathf.Abs( pitch ) > 0 ) {
offset = Quaternion.Euler( new Vector3( pitch, 0, 0 ) ) * offset;
rotated = true;
}
if( Mathf.Abs( yaw ) > 0 ) {
offset = Quaternion.Euler( new Vector3( 0, yaw, 0 ) ) * offset;
rotated = true;
}
if( relativeCamera ) transform.LookAt( target );
return rotated;
}
private float RotToDeg( float rot ) {
float deg = 0;
if( rot > 180f ) {
deg = rot - 360f;
} else {
deg = rot;
}
return deg;
}
}
note that in this script the target was the car body that was a child of a parent rigidbody that the wheels were also attached to, so you may need to modify the code so it targets the correct object in your scene.
@streeetwalker Thank you very much for your answer but my game is for mobile and I did a test with your script but I have the same problem
https://we.tl/t-Lf8eNgo0Yr thats the video with my problem(with my script)
As you can see is shakeing to much . And my camera system is not a child for a car
Thank you for your reply.
Apparently it helps if you put the whole camera system to track the car and add in that system an empty game object for look at. Thats the reason why in the first person dosent jitter, because in first person is a different look at target
ahh - sorry yes, this is a third person camera. I think you are looking at two different problems:
1 has to do with the Update loop not matching FixedUpdate - you can see that for a moment, I think, in the overhead of the car when the car is moving towards the camera - you see a pretty extreme jitter in the car. If you haven't read this resource it may help you if that is part of the problem: https://www.kinematicsoup.com/news/2016/8/9/rrypp5tkubynjwxhxjzd42s3o034o8
is the oscillation of the spring system as it goes over bumps, but that should have a $$anonymous$$imal effect a 3rd person camera if you are using code that does damping such as in the code of the one I posted.
Thank you very much for helping me with this problem which I finally managed to solve.
So what was the problem in the end?
Apparently it helps if you add an empty game object witch the camera can track, without tracking the position of the car with rigidbody, wheel colliders, ...
Follow this Question
Related Questions
When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer
When attaching my custom camera script camera shakes when player starts to move fast. 0 Answers
How Use rigidbody.MovePosistion With A Player Controlled Moving Camera 0 Answers
Help with setting up 2D auto scrolling camera using Cinemachine 1 Answer