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

[Solved] Help with moving player to center lanes when reversing direction too quickly

Discussion in 'Scripting' started by Alien_J, Nov 10, 2020.

  1. Alien_J

    Alien_J

    Joined:
    May 29, 2019
    Posts:
    67
    In the game, there is a path with 5 lanes. The Rigidbody Player moves 1 Unit Left/Right when A/D key is pressed. The Player should stop at the center of each lane. And it does stop at the center of each lane. The problem I'm running in to is, if the player is moving left and you press the D key to reverse direction before the player stops at the center of a lane, it will overshoot the center of the next lane.

    Example: Player is at 0 on X axis. You press the A key. Player begins to move left toward -1.
    But before the player stops at that -1 position, let's say it was at -0.6, you press the D key. The player will reverse direction and stop at 0.6.

    I'm trying to figure out how to move the player to the center of each lane regardless of its position at the time another key is pressed.

    This is my code so far:

    Code (CSharp):
    1. {
    2.     public float mSpeed;
    3.  
    4.     private Rigidbody rb;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         //For Moving Left
    16.         if (Input.GetKeyDown(KeyCode.A))
    17.             StartCoroutine(MoveToPosition(rb.transform, new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), mSpeed));
    18.  
    19.         //For Moving Right
    20.         if (Input.GetKeyDown(KeyCode.D))
    21.             StartCoroutine(MoveToPosition(rb.transform, new Vector3(transform.position.x + 1, transform.position.y, transform.position.z), mSpeed));
    22.  
    23.         Debug.Log(transform.position);
    24.     }
    25.  
    26.     public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
    27.     {
    28.         var currentPos = transform.position;
    29.         var t = 0f;
    30.         while (t < 1)
    31.         {
    32.             t += Time.deltaTime / timeToMove;
    33.             transform.position = Vector3.Lerp(currentPos, position, t);
    34.             yield return null;
    35.         }
    36.     }
    37. }
    If any one can help with this, I'd be greatly appreciated. And I may throw you a few bucks. But you'll need a paypal accnt.
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Just don't use your player current position. Create a variable to store the position at which you should be.
    Code (CSharp):
    1. public float mSpeed;
    2.     private Rigidbody rb;
    3.     private float xPos;
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         rb = GetComponent<Rigidbody>();
    8.         xPos = transform.position.x;
    9.     }
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         //For Moving Left
    14.         if (Input.GetKeyDown(KeyCode.A))
    15.             StartCoroutine(MoveToPosition(rb.transform, new Vector3(xPos - 1, transform.position.y, transform.position.z), mSpeed));
    16.         //For Moving Right
    17.         if (Input.GetKeyDown(KeyCode.D))
    18.             StartCoroutine(MoveToPosition(rb.transform, new Vector3(xPos + 1, transform.position.y, transform.position.z), mSpeed));
    19.         Debug.Log(transform.position);
    20.     }
    21.     public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
    22.     {
    23.         var currentPos = transform.position;
    24.         var t = 0f;
    25.         xPos = position.x;
    26.         while (t < 1)
    27.         {
    28.             t += Time.deltaTime / timeToMove;
    29.             transform.position = Vector3.Lerp(currentPos, position, t);
    30.             yield return null;
    31.         }
    32.     }
     
    Alien_J likes this.
  3. Alien_J

    Alien_J

    Joined:
    May 29, 2019
    Posts:
    67
    All Hail @LilFire

    Works perfectly!

    I was thinking I needed to create a variable for the "GOTO Pos", just didn't know/understand how to get that done.

    Thank you so much for actually replying, and helping me with this. Now I can finally move on to the next step.
     
    Last edited: Nov 11, 2020