- Home /
The question is answered, right answer was accepted
Jittering Camera 3D ,Jittery 3D camera using a LookAt() on an object
I have a simple project that I'm working on for school coursework and I'm using a LookAt() at the object. However, when I rotate move the camera the object jitters horribly. I've looked on the forums and tried solutions but I can't find anything. I'll post the code as well as the video. Thank you all for your time!
public class CameraMovement : MonoBehaviour
{
// Start is called before the first frame update
public Transform target;
public float rotationSpeed;
public float normalFOV;
public float zoomedFOV;
public int smooth;
void Start()
{
normalFOV = 100f;
zoomedFOV = 10f;
rotationSpeed = 10f;
smooth = 5;
this.GetComponent<Camera>().fieldOfView = normalFOV;
}
// Update is called once per frame
void FixedUpdate()
{
transform.LookAt(target);
if (Input.GetMouseButton(1))
{
MouseRotation();
};
MouseZoom();
}
private void MouseRotation()
{
Vector3 cameraV = new Vector3(
Input.GetAxis("Mouse X"),
Input.GetAxis("Mouse Y")
);
transform.Translate(cameraV * Time.deltaTime * rotationSpeed);
}
private void MouseZoom()
{
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0f && this.GetComponent<Camera>().fieldOfView != zoomedFOV)
{
this.GetComponent<Camera>().fieldOfView -= smooth;
}
if (Input.GetAxisRaw("Mouse ScrollWheel") < 0f && this.GetComponent<Camera>().fieldOfView != normalFOV)
{
this.GetComponent<Camera>().fieldOfView += smooth;
}
}
,I have this simple camera that I use to focus on an object. However, every time I do rotate it, it causes really jittery camera movement. I will paste the code I've used for the camera below.
public class CameraMovement : MonoBehaviour
{
// Start is called before the first frame update
public Transform target;
public float rotationSpeed;
public float normalFOV;
public float zoomedFOV;
public int smooth;
void Start()
{
normalFOV = 100f;
zoomedFOV = 10f;
rotationSpeed = 10f;
smooth = 5;
this.GetComponent<Camera>().fieldOfView = normalFOV;
}
// Update is called once per frame
void FixedUpdate()
{
transform.LookAt(target);
if (Input.GetMouseButton(1))
{
MouseRotation();
};
MouseZoom();
}
private void MouseRotation()
{
Vector3 cameraV = new Vector3(
Input.GetAxis("Mouse X"),
Input.GetAxis("Mouse Y")
);
transform.Translate(cameraV * Time.deltaTime * rotationSpeed);
}
private void MouseZoom()
{
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0f && this.GetComponent<Camera>().fieldOfView != zoomedFOV)
{
this.GetComponent<Camera>().fieldOfView -= smooth;
}
if (Input.GetAxisRaw("Mouse ScrollWheel") < 0f && this.GetComponent<Camera>().fieldOfView != normalFOV)
{
this.GetComponent<Camera>().fieldOfView += smooth;
}
}
}
I will also attach a video to demonstrate the problem.
Thank you very much for your time and I tried to search the forums but I couldn't find anything to help me, I had already tried to use FixedUpdates etc, it may be just that the code isn't really optimum. Again, thank you very much!
honestly, common problem, happens everytime I did a camera follower, because update frame by frame is "seen" by human eye, get why happen each time, is a travel so, start, change
void FixedUpdate(){
to
void LateUpdate(){
check if this $$anonymous$$imize the jittering
and (off the question) to $$anonymous$$ouseRotation
I suggest to use the RotateAround method
transform.RotateAround(target.position, target.up, Input.GetAxis("$$anonymous$$ouse X")* rotationSpeed);
transform.RotateAround(target.position, target.right, Input.GetAxis("$$anonymous$$ouse Y")*rotationSpeed);
to get a "circular camera panning" around the target
Thank you I'll try what you've said later when I get home.
Works completely, wow. Thank you for your time and now I know how to use the camera!