Need help with Camera Jittery Problem ?

I have a very simple script to move the camera left and right (using “A” or “D” for “left” and “right”).

I notice that the camera has a jittery problem,
anyone know what might causing the problem ?
(it looks worse in Firefox, but slightly ok with Safari)

this only happens in the Web version, it’s not happening if i build the executeable version (OS X).

Here is what i tried so far:
the scene itself is very simple, only one camera and couple houses, no other script running (except that FramePerSecond script)

1. First Try:
using this script and assign it to Camera

function SimpleInput()
{
		if (Input.GetButton("left"))
		{
			transform.Translate(-Time.deltaTime * 5, 0, 0);
		}else if (Input.GetButton("right"))
		{
			transform.Translate(Time.deltaTime * 5, 0, 0);		
		}	
}

function Update () {
	SimpleInput();
}

Result:
http://anicombo.com/unity/CameraJitter.html

2. Second Try:
using InputAxis instead of GetButton

function AxisInput()
{
	var moving = Input.GetAxis ("Horizontal") * 5;
	moving *= Time.deltaTime;

	transform.Translate (moving, 0, 0);	
}

function Update () {
	AxisInput();
}

Result:
http://anicombo.com/unity/CameraAxis.html

3. And last try,
this one is slightly different, instead of actually moving the camera i’m using “SmoothFollow.js” script, i make an empty object as my target, and assign it to the Camera, and moving the empty object (target) instead of the camera itself.

Result:
http://anicombo.com/unity/CameraSmoothFollow.html

Looks like all of them have the same jittery problem,
anyone could help me find the solution ?
thanks a lot in advance :smile:

Sure. Type ‘camera AND jitter’ into the forum’s search form. Then click on the first item returned in the result list. The solution will be at the top of the page. :shock:

thanks Quietus, actually i saw that thread already, i always make sure to do a search first before posting a question so that it won’t add unnecessary noise on the forum :wink:

following the suggestion to move the script to FixedUpdate doesn’t really fix the problem, here is what happen when i move the SimpleInput to FixedUpdate:
http://anicombo.com/unity/FixedUpdate.html

and here is if i move it to LateUpdate:
http://anicombo.com/unity/LateUpdate.html

I did another try by averaging the distance traveled, between previous frame and current frame
(not sure if this is the right way to do it though)

private var oldDistance = 0.0;
private var newDistance = 0.0;

function Awake()
{
	oldDistance = 0.0;
	newDistance = 0.0;
}

function AvgDistance()
{
		if (Input.GetButton("left"))
		{
			newDistance = Mathf.Lerp(oldDistance, (5 * Time.deltaTime), 0.5);
			transform.Translate(-newDistance, 0, 0);
			oldDistance = newDistance;
		}else if (Input.GetButton("right"))
		{
			newDistance = Mathf.Lerp(oldDistance, (5 * Time.deltaTime), 0.5);			
			transform.Translate(newDistance, 0, 0);			
			oldDistance = newDistance;			
		}
}

function Update()
{
   AvgDistance();
}

result is still pretty much the same:
http://anicombo.com/unity/LerpMove.html

I’m out of idea right now, so any help is really appreciated :smile:
for now i guess i’ll just have to live with this.

Ok I misinterpreted. You’re just physically moving the camera, not getting jitters when having the camera track an object.

It also feels like z-fighting, more than an actual camera jitter. That is, at least what’s noticeable on Safari. Are the houses built from 1 sided planes?

thanks again Quietus :smile:, yes i just physically moving the camera left right using “A” and “D” key with 5 as the speed on X axis only.

  • maybe i should remove the ground before i test it again. Good thing the z-fighting only happens on the ground (because i just simply duplicate it and put it side by side :P, the ground is the only part that overlap each other and causing Z-fighting).

  • the house is just a simple textured box, i’ll do more test tonight using a primitive instead of the actual model, so there won’t be any overlapping on the ground.
    Hopefully i’ll see some improvement, i’ll keep this thread updated with more test later in case someone else facing the same problem.

So here’s is my last try, using
a simple Box as the scene,
but the problem still persist :

http://anicombo.com/unity/CameraMove.html

if anyone wants to check the scene, i put it on the attachment. (a simple once scene with one script)

Here is the scripts, all function will have the same jittery problem :stuck_out_tongue:

private var oldDistance = 0.0;
private var newDistance = 0.0;

function Awake()
{
	oldDistance = 0.0;
	newDistance = 0.0;
}

function SimpleInput()
{
		if (Input.GetButton("left"))
		{
			transform.Translate(-Time.deltaTime * 5, 0, 0);
		}else if (Input.GetButton("right"))
		{
			transform.Translate(Time.deltaTime * 5, 0, 0);		
		}
}

function ConstantInput()
{
		if (Input.GetButton("left"))
		{
			transform.Translate(-0.12, 0, 0);
		}else if (Input.GetButton("right"))
		{
			transform.Translate(0.12, 0, 0);
		}
}

function AxisInput()
{
	var moving = Input.GetAxis ("Horizontal") * 5;
	moving *= Time.deltaTime;

	transform.Translate (moving, 0, 0);	
}

function AverageInput()
{
		if (Input.GetButton("left"))
		{
			newDistance = Mathf.Lerp(oldDistance, (5 * Time.deltaTime), 0.5);
			transform.Translate(-newDistance, 0, 0);
			oldDistance = newDistance;
		}else if (Input.GetButton("right"))
		{
			newDistance = Mathf.Lerp(oldDistance, 5 * Time.deltaTime, 0.5);			
			transform.Translate(newDistance, 0, 0);			
			oldDistance = newDistance;			
		}
}

function Update () {
	SimpleInput();
//	ConstantInput();
//	AxisInput();	
//	AverageInput();
}

maybe moving the camera like this is a bad idea :cry:

118744–4505–$cameramove_180.zip (508 KB)

i just checked on the Island Demo:

especially when you go to this part (where there is not much obstacle so you can strafe LEFT/RIGHT in a long way)

looks like the camera also has a slight jitter (when you strafing to LEFT/RIGHT),
is this problem really bound to happen when you move the camera Left/Right ??

Maybe the “jitter” is screen tearing, in which case that’s solved by using vsync.

–Eric

Thanks Eric, you’re right… 8)
looks like once i turn on the Vsync, it doesn’t have
the tearing (which i thought was jittering at first).

here is for comparison, with Vsync On:
http://anicombo.com/unity/VsyncOn.html

VSync Off:
http://anicombo.com/unity/VsyncOff.html

the stuttering still happens on both of them though,
which i think is a whole another problem in itself :?
(i still don’t know why that happens :P)

The camera will jitter no matter what you do. We have tried every combination of deltaTimes and Updates in relatively simple scenes, in stand-alone players, web players, and the editor; with targeted and free fps, with V-sync enabled and not.

The jitter is one of the first things I notice in every Unity game or demo on the PC, so I think its inherent.

I would love to hear differently.

I got the same problem. These jitters don’t allow smooth Camera movement and thereby reduce the quality of perception for the gamer. Quite demoralising…

Average the input over a set number of frames?
This script will remove that weird jittery mouse movement you get in far too many Unity games, maybe something similar can be used to fix whatever problem you guys are having.

I always drive my cameras with a smoothFollow empty object and smoothLookAt for targeting.

function Update ()
{
//Mouse/Camera Movement Smoothing:	
//Average rotationX for smooth mouselook
	var rotAverageX : float = 0;
	
	rotationX += Input.GetAxis("Mouse X") * sensitivityX;
	
    //Add the current rotation to the array, at the last position
    rotArrayX[rotArrayX.length] = rotationX;

    //Reached max number of steps?  Remove the oldest rotation from the array
    if (rotArrayX.length >= frameCounterX) 
    {
        rotArrayX.RemoveAt(0);
    }

    //Add all of these rotations together
    for (var i_counterX = 0; i_counterX < rotArrayX.length; i_counterX++) 
    { //Loop through the array
        rotAverageX += rotArrayX[i_counterX];
    }

    //Now divide by the number of rotations by the number of elements to get the average
    rotAverageX /= rotArrayX.length;
   
//Average rotationY, same process as above
	var rotAverageY : float = 0;	
	rotationY += Input.GetAxis("Mouse Y") * sensitivityY;	
    rotArrayY[rotArrayY.length] = rotationY;
    if (rotArrayY.length >= frameCounterY) 
    {
        rotArrayY.RemoveAt(0);
    }
    for (var i_counterY = 0; i_counterY < rotArrayY.length; i_counterY++) 
    {
        rotAverageY += rotArrayY[i_counterY];
    }
    rotAverageY /= rotArrayY.length;

//Set Rotation Limits for vertical    
//    if ((rotAverageY >= -360)  (rotAverageY <= 360))
//    {
//        rotAverageY = Mathf.Clamp (rotAverageY, minimumY, maximumY);
//    }
//    else if (rotAverageY < -360)
//    { 
//        rotAverageY = Mathf.Clamp (rotAverageY+360, minimumY, maximumY);
//    }
//    else
//    {
//        rotAverageY = Mathf.Clamp (rotAverageY-360, minimumY, maximumY);
//    }

//Apply and rotate this object
    xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
    yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);

    transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}

The in-game demo video here http://sdunne.carbonmade.com/ features some movement with that script BUT the screen capture software cause some framerate stuttering so might not be much use.

Here’s the full script with syntax highlighting, it has some aim assist code in there too http://pastebin.com/E0EdbVsd