Move the main camera.

Hello, is me again. :smile:

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.

void Update()
    { // Inicio del metodo <Update>

        if (Input.GetKeyDown (KeyCode.RightArrow))
                        Camera.main.transform.Translate (10, 0, 0);
        if (Input.GetKeyUp (KeyCode.RightArrow))
                        Camera.main.transform.Translate (-10, 0, 0);
        if (Input.GetKeyDown (KeyCode.LeftArrow))
                        Camera.main.transform.Translate (-10, 0, 0);
        if (Input.GetKeyUp (KeyCode.LeftArrow))
                        Camera.main.transform.Translate (10, 0, 0);

        if (Input.GetKeyDown (KeyCode.UpArrow))
                        Camera.main.transform.Translate (0, 10, 0);
        if (Input.GetKeyUp (KeyCode.UpArrow))
                        Camera.main.transform.Translate (0, -10, 0);
        if (Input.GetKeyDown (KeyCode.DownArrow))
                        Camera.main.transform.Translate (0, -10, 0);
        if (Input.GetKeyUp (KeyCode.DownArrow))
                        Camera.main.transform.Translate (0, 10, 0);

    } // Fin del metodo <Update>

The script is attached to the main camera. Will this be a viable option?

And thanks for everything.

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

 Camera.main.transform.Translate (Input.GetAxisRaw("Horizontal")*10,Input.GetAxisRaw("Vertical")*10, 0);

good luck

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

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.UpArrow)
           || Input.GetKeyDown(KeyCode.DownArrow)
           || Input.GetKeyDown(KeyCode.LeftArrow)
           || Input.GetKeyDown(KeyCode.RightArrow))
            Camera.main.transform.Translate (Input.GetAxisRaw("Horizontal")*10,Input.GetAxisRaw("Vertical")*10, 0);  
    }

Thanks for the anwser.

But this not affect the player move; the player have an script and move with this:

float controlHorizontal = Input.GetAxis("Horizontal");
        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.

Yep i move in vertical but no return to the base (the player) and horizontal dont work :smile: becouse i move the alternative key. is a good way.

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

1 Like

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

float moverCamX = Input.GetAxisRaw ("HorizontalCamara") * (Time.deltaTime * 250);
        float moverCamY = Input.GetAxisRaw("VerticalCamara") * (Time.deltaTime * 250);
       
        Camera.main.transform.Translate (moverCamX, 0, 0);
        Camera.main.transform.Translate (0, moverCamY, 0);

Thanks :slight_smile: