Search Unity

A little bit of help with transform.position please.

Discussion in 'Scripting' started by petizero, Feb 16, 2020.

  1. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Hi!

    Long story short I want to make a system that works like so:

    If i press space then the main camera would move on the x axis for - 20.052
    and the if i press space again it goes back to 0.

    Problem is i can't quite understand transform.position I can handle the rest of the stuff.
     
  2. TheGameNewBie

    TheGameNewBie

    Joined:
    Jul 27, 2017
    Posts:
    92
    One way to do this would be using a bool as a flag
    Code (CSharp):
    1. bool moved;
    2.  
    3. void Update () {
    4.           if(Input.GetKeyUp(KeyCode.Space)) {
    5.                   if(moved) {
    6.                         transform.position = new Vector3(0f, 0f, 0f);
    7.                         moved = false;
    8.                    } else if (!moved) {
    9.                          transform.position = new Vector3(-20f, 0f, 0f);
    10.                          moved = true;
    11.                   }
    12.             }
    13. }
    14. }
     
  3. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    For some reason i can't see what's at -20 after moving the cam
     
  4. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Nevermind fixed it issue was the camera by default is on -10 on the Z