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

3 position moving - Help

Discussion in '2D' started by tennisboy08, Feb 16, 2015.

  1. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    So i'm pretty new to unity and I've been trying to make it so my character will move from one of the three lanes to the one next to it using the a and d key
    The order of the lanes from left to right are A B C
    I cannot seem to get it to move from B to A
    Even going from A to B works

    Here is my code:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var smooth : float = 8;
    4.  
    5. private var newPosition : Vector3;
    6.  
    7.     var positionA : Vector3 = new Vector3(-3.5, 1, 0);
    8.     var positionB : Vector3 = new Vector3(0, 1, 0);
    9.     var positionC : Vector3 = new Vector3(3.5, 1, 0);
    10.  
    11. function Awake ()
    12. {
    13.  
    14.     newPosition = transform.position;
    15.  
    16.  
    17.  
    18.     newPosition = positionB;
    19. }
    20.  
    21.  
    22. function Update ()
    23. {
    24.     PositionChanging();
    25. }
    26.  
    27.  
    28. function PositionChanging ()
    29. {
    30.  
    31.  
    32.  
    33.         if(newPosition == positionA)
    34.         {
    35.             if(Input.GetKeyDown(KeyCode.D))
    36.             newPosition = positionB;
    37.         }
    38.         else if(newPosition == positionB)
    39.         {
    40.             if(Input.GetKeyDown(KeyCode.D))
    41.             newPosition = positionC;
    42.         }
    43.         else if(newPosition == positionB)
    44.         {
    45.             if(Input.GetKeyDown(KeyCode.A))
    46.             newPosition = positionA;
    47.         }
    48.         else if(newPosition == positionC)
    49.         {
    50.             if(Input.GetKeyDown(KeyCode.A))
    51.             newPosition = positionB;
    52.         }
    53.     transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    54. }
    55.  
     
  2. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    The problem is you have two else If's together that are both looking for the same thing (PositionB)

    Try:
    Code (CSharp):
    1.  
    2.  
    3. if(newPosition == positionA)
    4.         {
    5.             if(Input.GetKeyDown(KeyCode.D))
    6.             newPosition = positionB;
    7.  
    8.         }else if(newPosition == positionB)
    9.  
    10.         {
    11.             if(Input.GetKeyDown(KeyCode.A))
    12.             newPosition = positionA;
    13.      
    14.             if(Input.GetKeyDown(KeyCode.D))
    15.             newPosition = positionC;
    16.  
    17.         } else if(newPosition == positionC)
    18.  
    19.         {
    20.             if(Input.GetKeyDown(KeyCode.A))
    21.             newPosition = positionB;
    22.         }
    23.  
     
  3. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Thank you very much!