- Home /
Other
Camera shaking/jittering?
When my player is in the air, the camera decides to shake like crazy. Please view the video here :
https://www.youtube.com/watch?v=Z1A0Fvs9vlc&feature=youtu.be
When im on the floor, it works fine. I have tried using other forms of update and moving around code but nothing seems to work. Also, Player is a rigidbody ( Sphere ) if that helps using UnityEngine; using System.Collections;
public class camera_controller : MonoBehaviour {
public Transform PlayerTransform;
private Vector3 _cameraOffset;
public float playerRotation;
[Range(0.01f, 1.0f)]
public float SmoothFactor = 0.5f;
public bool LookAtPlayer = false;
void Start(){
_cameraOffset = transform.position - PlayerTransform.position;
}
void Update (){
transform.eulerAngles = new Vector3(0, PlayerTransform.transform.eulerAngles.y,0);
}
void LateUpdate (){
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
if(LookAtPlayer){
transform.LookAt(PlayerTransform);
}
}
}
Answer by JDelekto · Apr 28, 2019 at 10:27 AM
I would look at one of the Quaternion functions (such as RotateTowards) instead of modifying the camera transform's position. You can set the camera's rotation instead so it follows much smoother.
Hi thanks for the reply. Im not sure how i would implement this into my code, I have tried a few things but cannot figure it out for the life of me, Could you please add the line required? Thanks again.
Give this a try:
using UnityEngine;
public class LookAt : $$anonymous$$onoBehaviour
{
public Transform target;
private void LateUpdate()
{
Vector3 lookVector = target.position - transform.position;
transform.rotation = Quaternion.LookRotation(lookVector);
}
}
Edit: I used the LookRotation() ins$$anonymous$$d, it turned out to be easier. :) Edit2: Used LateUpdate() ins$$anonymous$$d of Update thanks to @TreyH 's advice.
Answer by TreyH · Apr 28, 2019 at 12:25 PM
This is probably because your camera_controller script is being executed before your movement.
Go into Edit >> Project Settings >> Script Execution Order and ensure that your camera script is last on that list.
Interesting! I hadn't even considered script execution order, is this common to do?
It can simplify some ti$$anonymous$$g issues, so things using a singleton pattern can happen first to ensure they'll be reliable for whatever's using them. Similarly, things like camera movement are recommended to happen in LateUpdate for the same reason. :-)
I'd go for the LateUpdate() myself, in fact, I'm editing my reply. Adjusting the script order, while it makes sense, is probably something that I would easily forget to do.
Hey guys, Thank you for the replies! Unfortunately these still dont fix it, it still does that crazy rotating issue with the background the faster i fall to the floor, Do you think its a script issue or something completely different ? thanks heaps! edit: ALSO i should mention the script isnt being directly applied to the camera, its applied to an empty game object so i can get the correct rotation for the camera...
Is there a reason you're rotating the camera in Update()? You are already using LookRotation in LateUpdate().
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object move on z axis while giving force on x axis 0 Answers
Issue w/ Difference between Cube and Capsule 0 Answers
How to limit an object's position to a spherical radius? 0 Answers