I am trying to script some boundaries for my camera object but I for the life of me cannot get it right. I tried using Bounds() but cant get the errors out. How should I go about doing this?
Probably Mathf.Clamp() would be the simplest. Bounds is for reading/setting bounding box volumes for objects. --Eric
Maybe I misundertsand what you are trying to do, but couldn't you attach some geometry to your camera, like the pre-fab first person controller, then create geometry that matches your boundaries and make that a mesh collider?
I have a camera that moves in the x and z directions over a terrain, but i want it to stop when or near when the terrain stops. Ive tried a couple of things but overall they have been failures. I think I misunderstood what bounds are used for. Im still learning so any help is most appreciated.
You should be able to put up 4 box colliders at each edge of the terrain and a box or sphere collider on the camera along with a rigidbody set to kinematic. Then the camera will get notified when the collision happens and you can stop moving the camera. This is just one way to do this quickly.
I'd recommend just using Mathf.Clamp. Code (csharp): camPos.x = Mathf.Clamp(camPos.x, leftBoundary, rightBoundary); camPos.z = Mathf.Camp(campPos.z, nearBoundary, farBoundary); That's assuming you've got a variable called camPos that has the camera's transform (var camPos = Camera.main.transform would do it), and that you've made the various Boundary variables and assigned appropriate values to them. If your terrain is square and centered on the origin, then you could just use one variable that's half the width of the terrain. --Eric
Code (csharp): var speed = 5.0; function Update () { var camPos = Camera.main.transform; var leftBoundary = 1500; var rightBoundary = 1600; var nearBoundary = 1490; var farBoundary = 910; camPos.x = Mathf.Clamp(camPos.x, leftBoundary, rightBoundary); camPos.y = Mathf.Clamp(camPos.z, nearBoundary, farBoundary); var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; var z = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(x, 0, z); } Am I close? I know coding is not a strong point of mine yet. I get an error saying "x" is not a member of UnityEngine.transform
Somewhat close. The variables need to be global, so they'd be declared outside Update. This way you can also edit the values in the Inspector, as you probably know. Also, since it looks like your script is operating on the camera itself, there isn't really a need to get Camera.main after all (it should have been camPos.position.x anyway; sorry). The clamping should be done after the Translate, so the numbers stay within the range you've specified for that frame. Otherwise they'd be clamped the next frame, which probably would lead to some jittering if you tried to go past the boundaries. Code (csharp): var speed = 5.0; var leftBoundary = -1500; // ? var rightBoundary = 1600; var nearBoundary = -1490; // ? var farBoundary = 910; function Update () { var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; var z = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(x, 0, z); transform.position.x = Mathf.Clamp(transform.position.x, leftBoundary, rightBoundary); transform.position.z = Mathf.Clamp(transform.position.z, nearBoundary, farBoundary); } I also changed the leftBoundary and nearBoundary values to be negative; near has to be less than far in any case. I'm not sure what your terrain looks like, but leftBoundary is the lowest x value in world space that you can go to, nearBoundary is the lowest z value, right is highest x and far is highest z. --Eric