Search Unity

2D Movements problem

Discussion in '2D' started by KiviGameMaker, Mar 18, 2018.

?

How To Make 3 Paths To Move A Player

  1. Everyone

    0 vote(s)
    0.0%
  2. All

    0 vote(s)
    0.0%
  1. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
    Hello guys i have little problem. In my game i have 3 lines and 1 Player . Line-1 is left , Line0 is starting Line and she is in middle of the screen, Line 1 is right.
    I need that when I click on A, the Player moves from Line0 to Line-1 when I click on D so the player moves from Line-1 to Line0 and again clicks D to move from Line-0 to Line1.
    I dont know how to script it. THX for every answer.
    Here is Link https://ibb.co/fWLKSx
     
  2. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    You can try with array. Add this script to player. Drag&drop your lines from the scene into this script (fill the array with lines from left to right). Set the number for first line (beginn with 0).

    Code (CSharp):
    1. //array with lines positions
    2.     public Transform [] Lines;
    3.     //first line where player will be at start (beginns with 0)
    4.     public int playerLine;
    5.  
    6.     void Start ()
    7.     {
    8.         //check that index isn't out of range (as example: your 3 lines will be indexed as 0, 1, 2)
    9.         playerLine = Mathf.Clamp(playerLine, 0, Lines.Length - 1);
    10.         //place player at needed line
    11.         transform.position = new Vector2(Lines[playerLine].position.x, transform.position.y);
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         //check if D is pressed
    17.         if (Input.GetKeyDown(KeyCode.D))
    18.         {
    19.             //increase the index at one
    20.             playerLine = playerLine + 1;
    21.             //check that new index can be out of range if greater as lenght of array -1
    22.             if (playerLine >= Lines.Length)
    23.                 //as said the index beginns with zero. The last line will have index: lenght of array - 1
    24.                 playerLine = Lines.Length - 1;
    25.             //move player at new line
    26.             transform.position = new Vector2(Lines[playerLine].position.x, transform.position.y);
    27.         }
    28.  
    29.         //check if A is pressed
    30.         if (Input.GetKeyDown (KeyCode.A))
    31.         {
    32.             //decrease the index at one
    33.             playerLine = playerLine - 1;
    34.             //check that new index isn't lower as 0
    35.             if (playerLine < 0)
    36.                 //set index to zero
    37.                 playerLine = 0;
    38.             //move player at new line
    39.             transform.position = new Vector2(Lines[playerLine].position.x, transform.position.y);
    40.         }
    41.     }
    And then check the unity tutorials :)
    https://unity3d.com/learn/tutorials
     
    KiviGameMaker likes this.
  4. KiviGameMaker

    KiviGameMaker

    Joined:
    Mar 18, 2018
    Posts:
    22
    Oh my God bro you help so much <3 than you very much