Search Unity

3 roads infinite runner problem of player movement

Discussion in 'Scripting' started by simonini_thomas, May 12, 2016.

  1. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    Hello !

    First I hope that I'm in the good place to ask, if not tell me.

    I want to create a simple 3D runner.

    But I have a problem, about the player movement because I have 3 roads (blue, green, red), and I want that when the player swipe (or click) he move only from one road to another. Consequently this is not free movements.

    My initial idea was to assign a number to each road : | -1 | 0 | 1 |. And create some conditions if you're on 1 and you click on right you will not move because there is no road after the red one.

    However it doesn't work very well.
    Picture :
    QuestionUnity.png

    Code (CSharp):
    1.   // If we click to left and the position of the player is 0 or 1
    2.  
    3.         if (Input.GetKey("left") && (playerPosition == 0 || playerPosition == 1))
    4.         {
    5.            
    6.             // Change direction to left
    7.                 endPosition = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
    8.             playerPosition = playerPosition - 1;
    9.            
    10.             Debug.Log("playerPosition Left : " + playerPosition);
    11.  
    12.         }
    13.         if (Input.GetKey("right") && (playerPosition == -1 || playerPosition == 0))
    14.         {
    15.  
    16.            
    17.             // Change direction to right
    18.             endPosition = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
    19.             playerPosition = playerPosition + 1;
    20.             Debug.Log("playerPosition Right : " + playerPosition);
    21.         }
    22.        
    23.  
    24.         gameObject.transform.position = Vector3.Lerp(direction, endPosition, speed);
    25.        
    26.      
    27.  
    28.     }
    29.  
    30. }
    Do you have some advices about how to move this player in relation to the road ?

    Thanks and have a good day !
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    First of all, Input.GetKey registers true on each frame the key is down. This means that to hit the center lane with your code, you'll have to hit the key for exactly one frame - which is almost impossible.

    Use GetKeyDown, which only returns true on the first frame after the key was pressed, that'll help with that.

    Your position code is also all wrong - you're lerping from direction to endPosition, which makes no sense - I don't think you understand what Lerp does. A good article about what Lerp is, and how to use it, is the "lerp like a pro" article linked in my signature.
     
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Also Multiply speed by Time.deltaTime for smooth movement on all systems ( framerate independent )