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

Move the main camera.

Discussion in '2D' started by WixoGTF, Jan 12, 2015.

  1. WixoGTF

    WixoGTF

    Joined:
    Dec 6, 2014
    Posts:
    8
    Hello, is me again. :D

    I am creating a script for camera move 10 meters (Unity Unit) in four directions with the arrow keys.

    After reading the manual this was my result works although I'm not entirely sure that's the right way or better performance.

    Code (CSharp):
    1. void Update()
    2.     { // Inicio del metodo <Update>
    3.  
    4.         if (Input.GetKeyDown (KeyCode.RightArrow))
    5.                         Camera.main.transform.Translate (10, 0, 0);
    6.         if (Input.GetKeyUp (KeyCode.RightArrow))
    7.                         Camera.main.transform.Translate (-10, 0, 0);
    8.         if (Input.GetKeyDown (KeyCode.LeftArrow))
    9.                         Camera.main.transform.Translate (-10, 0, 0);
    10.         if (Input.GetKeyUp (KeyCode.LeftArrow))
    11.                         Camera.main.transform.Translate (10, 0, 0);
    12.  
    13.         if (Input.GetKeyDown (KeyCode.UpArrow))
    14.                         Camera.main.transform.Translate (0, 10, 0);
    15.         if (Input.GetKeyUp (KeyCode.UpArrow))
    16.                         Camera.main.transform.Translate (0, -10, 0);
    17.         if (Input.GetKeyDown (KeyCode.DownArrow))
    18.                         Camera.main.transform.Translate (0, -10, 0);
    19.         if (Input.GetKeyUp (KeyCode.DownArrow))
    20.                         Camera.main.transform.Translate (0, 10, 0);
    21.  
    22.     } // Fin del metodo <Update>
    The script is attached to the main camera. Will this be a viable option?

    And thanks for everything.
     
  2. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    i think GetAxis("Horizontal") and GetAxis("Vertical") is more clean and return 1 or -1 the you can multiply for 10 and get 10 or -10 and put to your camera position and evuela :p.

    i dont try this code but something like thats i means
    Code (CSharp):
    1.  Camera.main.transform.Translate (Input.GetAxisRaw("Horizontal")*10,Input.GetAxisRaw("Vertical")*10, 0);
    good luck
     
    Last edited: Jan 12, 2015
  3. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    one thing, the last code is always reading the inputs if u want something more mmm step by step put some filter for example:

    Code (CSharp):
    1.  
    2.     void Update ()
    3.     {
    4.         if(Input.GetKeyDown(KeyCode.UpArrow)
    5.            || Input.GetKeyDown(KeyCode.DownArrow)
    6.            || Input.GetKeyDown(KeyCode.LeftArrow)
    7.            || Input.GetKeyDown(KeyCode.RightArrow))
    8.             Camera.main.transform.Translate (Input.GetAxisRaw("Horizontal")*10,Input.GetAxisRaw("Vertical")*10, 0);  
    9.     }
    10.  
     
  4. WixoGTF

    WixoGTF

    Joined:
    Dec 6, 2014
    Posts:
    8
    Thanks for the anwser.

    But this not affect the player move; the player have an script and move with this:
    Code (CSharp):
    1. float controlHorizontal = Input.GetAxis("Horizontal");
    2.         rigidbody2D.velocity = new Vector2 (controlHorizontal * velocidadMaxima, rigidbody2D.velocity.y);
    in fixedupdate method. i change the input key of horizontal to A and D with no alternative key and the vertical i dont gona use, and thats why i use the arrows key to move the camera.

    Im, gona trying and tell you what happend.
     
  5. WixoGTF

    WixoGTF

    Joined:
    Dec 6, 2014
    Posts:
    8
    Yep i move in vertical but no return to the base (the player) and horizontal dont work :D becouse i move the alternative key. is a good way.
     
  6. AlfonsoC

    AlfonsoC

    Joined:
    Oct 1, 2014
    Posts:
    27
    well if u are using horizontal and vertical for de player i think you can craate a axis por the camera ej: CameraHorizontal and CamaeraVertical and asign the arrows key and use this ones for the camera maybe dont affect to the player movemnt
     
    WixoGTF likes this.
  7. WixoGTF

    WixoGTF

    Joined:
    Dec 6, 2014
    Posts:
    8
    Yep early this morning (like 1am xD) i was read about delta time, transform.translate, input.getaxisraw and other things in the manual and i do that way. I create a new Input for Horizontal and Vertical only for the camara works like 50% is cool but i dont know why dont return to origin i was reading in Input if Snap combobox was enable the object (in this case the Camera) return to origin. So i gona continue works.

    New code move to FixedUpdate method
    Code (CSharp):
    1. float moverCamX = Input.GetAxisRaw ("HorizontalCamara") * (Time.deltaTime * 250);
    2.         float moverCamY = Input.GetAxisRaw("VerticalCamara") * (Time.deltaTime * 250);
    3.        
    4.         Camera.main.transform.Translate (moverCamX, 0, 0);
    5.         Camera.main.transform.Translate (0, moverCamY, 0);
    Thanks :)