Limit a camera

Hi,
I am triying to make a third person camera view in my game. I’m new to unity and programming, so it’s quite difficult to understand and make what I want.

Here is my problem : I made a script (that works quite well) to make the camera follow and rotate behind my character, but I would like to restrain te camera movement into a specific area.

Another problem is that I made this script to work with a joystick, that’s why I can’t manage with mouse scripts.

I’d like someone to help me finish this script. Thanks in advance.

Here is the script :

function Start () {

}

var speed = 10.0f;
 
function Update()
{
    var movement = Vector3.zero;
 
    movement.y = Input.GetAxis("RightV");
    movement.x = Input.GetAxis("RightH");
 
    transform.Translate(movement * speed * Global.deltaTime, Space.Self);
    
    return;
}

Nobody ?

I think we need more information, what do you mean by constrain it to a certian area?

I am going to make a little sketch to show it up :

Currently you have no limits to your transforms. Instead we want to adjust the local position based on the x and y motion.

movement.y += Input.GetAxis("RightV");
movement.x += Input.GetAxis("RightH");

movement.y = Mathf.Clamp(movement.y,lower,upper);
movement.x = Mathf.Clamp(movement.x,lower,upper);

transform.localPosition = movement * speed * Global.deltaTime;

That should do it. I may have some syntax errors since I program in C#, but the principles are there.

Thank you so much.

But I still have some problems. actually, the script worls as expected, only when the character is walking. When he’s idle, the camera gets too close to him and start spinning in some wierd way. I can figure out that the missing “space.self” could be the cause of this, but I can’t manage to fix it.

Please help

come on, please someone help me…

why would the camera rotate or move if you are not pressing anthing ? Have you update the camera in other place , let us see your whole codes

The whole code is there. The one in the first post rotates around the character when the right stick is used. No problem. But with the code Slev gave me, the camera is actually restrained as I want it to, but it sticks to the character and spins all alone.

If the script posted by slev is causing problems when you arent moving then you could cheat a little and disable it when you arent moving. if((Input.GetAxis("RightV") != 0 )(Input.GetAxis("RightH") !=0)) {do slevs code here}. This should be a cheap eficient and working quickfix. I dont really know where the problem came from because im replying from my phone but if i were to guess it was from the fact that axis when not moving have the value 0 wich could end up with tranform.localposition becoming 0.

thank you so much ! it works.