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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Position if Statement (2D)

Discussion in '2D' started by erzazockt, Mar 25, 2018.

  1. erzazockt

    erzazockt

    Joined:
    Mar 25, 2018
    Posts:
    1
    Hi guys I'm not English so please excuse my grammer. I have a Problem, maybe I'm just stupid:

    When I click a button then my Object moves to the right side. Now I want that when I click the same button again, it should move to the left side. Now i wanted to do an If Statement like

    if(this.transform.position.Equals(-0.81, -3.65, 0))

    But then it says me that equals is overloaded or something like that.
    Or when u have a completly other Code then please tell me.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    if (this.transform.position.Equals( new Vector3 (-0.81f, -3.65f, 0)))

    you can try to use something to modify the moving direction. As example:
    Code (CSharp):
    1.  
    2. public float speed;
    3. //or use boolean bool direction
    4. void Update () {
    5. if (Input.GetButtonDown ("Fire1"))
    6. //or an other key, like if (Input.GetKeyDown (KeyCode.A))
    7.         {
    8.             speed = speed * -1;
    9.             //or direction = !direction
    10.         }
    11.  
    12.         transform.Translate (Vector2.right * speed * Time.deltaTime);
    13.         //or with rigidbody rigidbody.velocity = new Vector2 (speed, 0)
    14. }
    15.  
     
  3. EdKirby

    EdKirby

    Joined:
    Nov 15, 2013
    Posts:
    43