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

Question Change xPos while also moving forward

Discussion in 'Scripting' started by Alien_J, Jun 26, 2023.

  1. Alien_J

    Alien_J

    Joined:
    May 29, 2019
    Posts:
    67
    My player has an "AutoMove" script and a "Change Lane" script attached to it. Both work. But when I change lanes the AutoMove script is overridden by the Chane Lane script. i.e. the player stops moving forward during the lane change.



    Auto Move
    Code (CSharp):
    1.  public class Player_Auto_Move : MonoBehaviour
    2. {
    3.  
    4.     public float speed = 0.05f;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         transform.Translate(Vector3.forward * speed * Time.deltaTime * 5);
    16.     }
    17.  
    18.    
    19. }
    Change Lane
    Code (CSharp):
    1. public class Player_XPos_Move : MonoBehaviour
    2. {
    3.    public float laneSpeed; //Change lane speed
    4.    private Rigidbody rb;
    5.    private float xPos;
    6.    private bool inputIsAllowed = true;
    7.    // Start is called before the first frame update
    8.    void Start()
    9.    {
    10.        rb = GetComponent<Rigidbody>();
    11.        xPos = transform.position.x;
    12.    }
    13.    // Update is called once per frame
    14.    void Update()
    15.    {
    16.        if (inputIsAllowed) //checks if inputs are allowed
    17.        {
    18.            //For Moving Left
    19.            if (Input.GetKeyDown(KeyCode.A))
    20.                StartCoroutine(MoveToPosition(rb.transform, new Vector3(xPos - 1, transform.position.y, transform.position.z), laneSpeed * Time.deltaTime * 5));
    21.            //For Moving Right
    22.            if (Input.GetKeyDown(KeyCode.D))
    23.                StartCoroutine(MoveToPosition(rb.transform, new Vector3(xPos + 1, transform.position.y, transform.position.z), laneSpeed * Time.deltaTime * 5));
    24.        }
    25.        Debug.Log(transform.position);
    26.    }
    27.    public IEnumerator MoveToPosition(Transform transform, Vector3 position, float timeToMove)
    28.    {
    29.        var currentPos = transform.position;
    30.        var t = 0f;
    31.        xPos = position.x;
    32.        while (t < 1)
    33.        {
    34.            t += Time.deltaTime / timeToMove;
    35.            transform.position = Vector3.Lerp(currentPos, position, t);
    36.            yield return null;
    37.        }
    38.        if (Mathf.Abs(xPos) > 2.0f)
    39.        {
    40.            inputIsAllowed = false;
    41.            Debug.Log(inputIsAllowed);
    42.        }
    43.    }
    44. }
    How can I get the player to change lanes while also moving forward at the same time? This will be a mobile game, just testing with keyboard for now.



    Any help is greatly appreciated.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    If you're going to have seperate components to define movement behaviour, you probably want another component that acts as your central API for moving the player.

    Though I don't see a strong reason why these need to be separate components at the moment.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Definitely DO NOT use Coroutines for this! Oh my goodness, bug city here.

    Plus (I assume) you want the user to be able to go from lane 1 to lane 3 by tapping D twice rapidly, right?? Personally I hate games that make you wait until you arrive in a lane to move! Nintendo games would NEVER do this... gotta make the movement feel smooth!!!

    Here's a super-simple example of lane-based motion:

    https://gist.github.com/kurtdekker/795ac387027c510548bcb635fac0caff

    Pick reasonable values for all the constants, give it a try.
     
    Alien_J and zulo3d like this.
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    If you're using a rigidbody then it's best to use forces. And you should try to combine movement into a single script.

    Code (CSharp):
    1. void Start()
    2. {
    3.     rb = GetComponent<Rigidbody>();
    4.     xPos=transform.position.x;
    5. }
    6.  
    7. // Input stuff in Update:
    8. void Update()
    9. {
    10.     if (Input.GetKeyDown(KeyCode.A) && transform.position.x>-2.0f)
    11.         xPos=xPos-1;
    12.     else if (Input.GetKeyDown(KeyCode.D) && transform.position.x<2.0f)
    13.         xPos=xPos+1;
    14. }
    15.  
    16. // physics stuff in FixedUpdate:
    17. void FixedUpdate()
    18. {
    19.     Vector3 forward=Vector3.forward*speed;    // constantly push the accelerator. add friction in editor to counter this
    20.     Vector3 sideways=Vector3.right*(xPos-transform.position.x)*speed*0.5f;
    21.     rb.AddForce(forward+sideways);
    22. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Not appropriate here. He wants lane-based movement.

    In his original code each tap of the key (I believe) is intended to discretely change which lane he is in, cycling through a known set of lanes.

    ALSO: it's usually better with Rigidbody to simply SET the velocity rather than using forces. Forces are great for physics simulation but rarely make for good arcade controllers.
     
  6. Alien_J

    Alien_J

    Joined:
    May 29, 2019
    Posts:
    67
    Thank you for the reply. I will definitely check it out after work.
     
  7. Alien_J

    Alien_J

    Joined:
    May 29, 2019
    Posts:
    67
    Exactly. Although it's been said Nintendo would never do it, I don't want the player to be able to move the character until they have arrived in that specific lane. Although the timing of how long it takes them to change lanes will shorten as the character speeds up.

    And thank you for the reply. I will research velocity.