Camera movement problem

Hey!

Im very new to programing so the question might be a bit stupid.

I have a camera wich rotates around the y axis with “Mouse X” and pans up and down with “mouse Y”. I would like the camera to stop paning when it reaches: transform.position.y = 1. All of this works!..but when u keep on paning down the camera starts shaking/lagging/hacking (or whatever to call it)…I would like the camera to just stop dead even if the user keeps on paning down :slight_smile:

Heres the code:

var horizontalSpeed : float = 2;
var verticalSpeed : float = 0.2;

function Update () {

	if(Input.GetButton("Fire1"))
	{
		var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
		var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
	}
	
	if(transform.position.y > 1)
	{
		transform.position.y = 1;
	}
	
	transform.position += -transform.up * v;	
	transform.Rotate(0, h, 0);
}

Move the transform.position and the transform.rotate lines to before the y bound check.

var horizontalSpeed : float = 2;
var verticalSpeed : float = 0.2;

function Update () {

	if(Input.GetButton("Fire1"))
	{
		var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
		var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
	}

	transform.position += -transform.up * v;	
	transform.Rotate(0, h, 0);

	if(transform.position.y > 1)
	{
		transform.position.y = 1;
	}
	
	
}

What’s happening is called state oscillation… you’re correcting for the upper bound before you assign the new position to the transform, so it will only correct the y limit for the current position when the script loops around on the next frame, and it will jitter visibly.

Cheers

Wow that was quick! Thanks!