I wrote this script for camera movement, now it goes with the mouse movement in the X and Y, and it zoom IN and OUT... BUT now i have 2 problems: 1- I can't force the movement to stop by the end of my terrain in the X,Y. 2- I want to control the zoom IN and OUT, cause it keeps zooming with the mouse wheel. How can I restrict those 2 options.. !!! (Thanks) Code (CSharp): using UnityEngine; using System.Collections; public class MoveCamera : MonoBehaviour { public GameObject cameraObject; public float speed = 1; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetAxis ("Mouse ScrollWheel") > 0) { cameraObject.transform.position= new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y +(1*speed) , cameraObject.transform.position.z); } if (Input.GetAxis ("Mouse ScrollWheel") < 0){ cameraObject.transform.position= new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y -(1*speed) , cameraObject.transform.position.z); } if (Input.mousePosition.x >= (Screen.width * 0.9)){ cameraObject.transform.position = new Vector3(cameraObject.transform.position.x +(1*speed),cameraObject.transform.position.y, cameraObject.transform.position.z); } if (Input.mousePosition.x <= (Screen.width*0.1)){ cameraObject.transform.position = new Vector3 (cameraObject.transform.position.x -(1*speed),cameraObject.transform.position.y, cameraObject.transform.position.z); } if (Input.mousePosition.y >= (Screen.height * 0.9)){ cameraObject.transform.position = new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y, cameraObject.transform.position.z + (1* speed)); } if (Input.mousePosition.y <= (Screen.height * 0.1)){ cameraObject.transform.position = new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y, cameraObject.transform.position.z - (1*speed)); } } }
If you have a limiting values in mind, I think Mathf.Min and Mathf.Max might be the solutions you are looking for. Alternatively you could add && (Condition) to your if statements.
I'd either use a separate pair of "if" statement (one for x and one for y), or use two Mathf.Clamp() statements (again, one for x direction and the other for y).
Thank you all .. i used Mathf.Clamp for X and Z.. Code (JavaScript): function Update () { var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20; transform.Translate(Vector3(xMove,0,0)); transform.position.x = Mathf.Clamp(transform.position.x, -120, 120); transform.position.z = Mathf.Clamp(transform.position.z, -206, 25); but Mathf, still can't use it for the mouse scroll .. when i tried, the camera acted weird and the scene disappeared.. any workout for this !!