Stop the camera from going up/down the y axis if it hit s the boarder

Hi folks

I have a little Problem

my camera moves around the x and z really fine until it hit s the boarder. Then it compensates and goes up or down the y axis :frowning:

Hope you can help me

Here the code:

var x = Input.GetAxis("Horizontal") * Time.deltaTime * camspeed;
	var z = Input.GetAxis("Vertical") * Time.deltaTime * camspeed;
	transform.Translate(x, 0, z);
	transform.position.x = Mathf.Clamp(transform.position.x, minPositionx, maxPositionx);
	transform.position.z = Mathf.Clamp(transform.position.z, minPositionz, maxPositionz);
	transform.position.y = Mathf.Clamp(transform.position.y, minPositiony, maxPositiony);

noone knows?

The code you posted never modifies the camera’s y position, so something else must be going on. Do you have any other code anywhere that may be translating the camera in Y? Are there any collider- or rigidbody- type components on the camera game object? You said, “until it hits the border.” What border? You need to post more information for anyone to diagnose the problem.

just use the LookMouse script provided by unity then use this code from anywhere in ur scene to change the cameras Axies view :

GameObject.Find(“your_camera_name”).GetComponent(“MouseLook”).RotationAxes =1;

// a value of 0 for Xand Y , 1 for X onky and 2 for Y only

You’re right. there is no actual code that modifies the Y axis. That’s why I don t know how to fix the problem.

The following code:

	var x = Input.GetAxis("Horizontal") * Time.deltaTime * camspeed;
	var z = Input.GetAxis("Vertical") * Time.deltaTime * camspeed;
	transform.Translate(x, 0, z);

is attached to the main camera. So if u use WASD you can move the camera around.

The following code:

var minPositionx : float = -5.6; // left border
var maxPositionx : float = 3.4; //  right border
var minPositionz : float = -6.5; // left border
var maxPositionz : float = 2.5; //  right border
var minPositiony : float = 0; // down
var maxPositiony : float = 10.0; //  up

        
transform.position.x = Mathf.Clamp(transform.position.x, minPositionx, maxPositionx);
	transform.position.z = Mathf.Clamp(transform.position.z, minPositionz, maxPositionz);
	transform.position.y = Mathf.Clamp(transform.position.y, minPositiony, maxPositiony);

creates a border so that the camera doesn’t move past this point. The problem is, if you reach the border with the camera and still hit like backwars the camera goes up the y axis instead of stopping.

Hope I made that clearer for you :slight_smile:

Assuming that the camera’s y-position starts between minPositiony and maxPositiony, there’s still nothing there that would cause it to move in y. Either you have more code you haven’t posted yet that’s causing the problem, or there’s something else going on as I said before.