Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera movement

Discussion in 'Scripting' started by GolfPro1, Jun 4, 2016.

  1. GolfPro1

    GolfPro1

    Joined:
    May 21, 2016
    Posts:
    37
    Hi,the code below will move the camera on the z and x axis only if the rotation of the camera is 0,however
    my camera is at a angle( x 30) and when moved along the z axis the camera also moves along the y axis because of the angle.
    Could you tell me how to move the camera along the z axis only with the camera at a angle as the angle is needed for my game but not the y movement when moved along the z axis.


    Code (csharp):
    1.  
    2. public class DragCamera : MonoBehaviour {
    3.  
    4.     public float speed = 10.0f;
    5.     // Use this for initialization
    6.     void Start () {
    7.    
    8.     }
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.    
    13.     Vector3 dir = new Vector3();   //create 0,0,0
    14.    
    15.     if(Input.GetKey(KeyCode.W))dir.z += 1.0f;
    16.     if(Input.GetKey(KeyCode.W))dir.y += 0;  //test restrict y movement does not work
    17.    
    18.     if(Input.GetKey(KeyCode.S))dir.z -= 1.0f;
    19.     if(Input.GetKey(KeyCode.S))dir.y -= 0; //test restrict y movement does not work
    20.    
    21.     if(Input.GetKey(KeyCode.A))dir.x -= 1.0f;
    22.     if(Input.GetKey(KeyCode.D))dir.x += 1.0f;
    23.    
    24.     dir.Normalize();
    25.    
    26.     transform.Translate(dir * speed * Time.deltaTime);
    27.    
    28.     }//end update
    29.  
    30.  
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Currently you're moving the camera relative to itself, so its Z-axis is wherever the camera is pointing. You want to move it relative to the world, where the Z-axis is always the same. Change the last line to this:

    Code (csharp):
    1. transform.Translate(dir *speed*Time.deltaTime, Space.World);
     
  3. GolfPro1

    GolfPro1

    Joined:
    May 21, 2016
    Posts:
    37
    Thats works perfect, thx v.much,
    also I would like to limit the movement to just the map so which method would be best
    to limit the movement on the x and z axis?
     
  4. GolfPro1

    GolfPro1

    Joined:
    May 21, 2016
    Posts:
    37
    Basic Camera movement complete,
    and again thx v.much for the help :)

    Code (csharp):
    1.  
    2.  
    3. public float speed = 10.0f;
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.    
    12.     Vector3 dir = new Vector3();   //create 0,0,0
    13.    
    14.     if(transform.position.z < 33f){
    15.     if(Input.GetKey(KeyCode.W))dir.z += 1.0f;
    16.     }
    17.     if(transform.position.z > -5f){
    18.     if(Input.GetKey(KeyCode.S))dir.z -= 1.0f;
    19.     }
    20.     if(transform.position.x > 0f){
    21.     if(Input.GetKey(KeyCode.A))dir.x -= 1.0f;
    22.     }
    23.     if(transform.position.x < 50f){
    24.     if(Input.GetKey(KeyCode.D))dir.x += 1.0f;
    25.     }
    26.    
    27.     dir.Normalize();
    28.    
    29.     transform.Translate(dir * speed * Time.deltaTime, Space.World);
    30.    
    31.     }//end update
    32.    
    33.  
    34. }
    35.  
    36.