Search Unity

Camera Movement

Discussion in 'Scripting' started by kha_erp, Sep 30, 2014.

  1. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveCamera : MonoBehaviour {
    5.  
    6.     public GameObject cameraObject;
    7.     public float speed = 1;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
    17.             cameraObject.transform.position= new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y +(1*speed) ,    cameraObject.transform.position.z);
    18.         }
    19.         if (Input.GetAxis ("Mouse ScrollWheel") < 0){
    20.             cameraObject.transform.position= new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y -(1*speed) , cameraObject.transform.position.z);
    21.         }
    22.         if (Input.mousePosition.x >= (Screen.width * 0.9)){
    23.             cameraObject.transform.position = new Vector3(cameraObject.transform.position.x +(1*speed),cameraObject.transform.position.y, cameraObject.transform.position.z);
    24.         }
    25.         if (Input.mousePosition.x <= (Screen.width*0.1)){
    26.             cameraObject.transform.position = new Vector3 (cameraObject.transform.position.x -(1*speed),cameraObject.transform.position.y, cameraObject.transform.position.z);
    27.         }
    28.         if (Input.mousePosition.y >= (Screen.height * 0.9)){
    29.             cameraObject.transform.position = new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y, cameraObject.transform.position.z + (1* speed));
    30.         }
    31.         if (Input.mousePosition.y <= (Screen.height * 0.1)){
    32.                 cameraObject.transform.position = new Vector3(cameraObject.transform.position.x, cameraObject.transform.position.y, cameraObject.transform.position.z - (1*speed));
    33.         }
    34.     }
    35. }
    36.  
    37.  
     
  2. danield

    danield

    Joined:
    Sep 30, 2014
    Posts:
    40
    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.
     
  3. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    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).
     
  4. kha_erp

    kha_erp

    Joined:
    Sep 21, 2014
    Posts:
    13
    Thank you all .. i used Mathf.Clamp for X and Z..
    Code (JavaScript):
    1. function Update () {
    2.  
    3.      var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
    4.      transform.Translate(Vector3(xMove,0,0));
    5.      transform.position.x = Mathf.Clamp(transform.position.x, -120, 120);
    6.      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 !!