Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Prevent player moving backwards

Discussion in 'Scripting' started by Aidy, Jun 29, 2015.

  1. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    Hey,

    I want to stop the player from moving back on themselves, basically. Here's the movement function:

    Code (CSharp):
    1. void Move()
    2.     {
    3.         //This'll change the gameobject's position
    4.         myBody.MovePosition(myTrans.position + myTrans.forward * speed * Time.deltaTime);
    5.  
    6.         //And this changes the character's movement direction based on the input given by the player
    7.         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
    8.         {
    9.             myTrans.forward = new Vector3(0f, 0f, 1f);
    10.             DirectionChanged();
    11.         }
    12.         else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
    13.         {
    14.             myTrans.forward = new Vector3(0f, 0f, -1f);
    15.             DirectionChanged();
    16.         }
    17.         else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
    18.         {
    19.             myTrans.forward = new Vector3(1f, 0f, 0f);
    20.             DirectionChanged();          
    21.         }
    22.         else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
    23.         {
    24.             myTrans.forward = new Vector3(-1f, 0f, 0f);
    25.             DirectionChanged();                      
    26.         }
    27.  
    28.         if (mostRecentTrail != null)
    29.             mostRecentTrail.Rescale (myTrans.position);
    30.     }
    The player would need to move up or down before they can move backwards again. My brain's just a little broken at the moment after working on getting the player's trail to work properly, so I can't think of the way to do this right now.

    Help would be appreciated c:
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,222
    You have a lot of input code for what is essentially 2 axis. Why not use GetAxis?

    Something like this

    Code (csharp):
    1. float x = Input.GetAxis("Horizontal");
    2. float y = Input.GetAxis("Vertical");
    3.  
    4. y = Mathf.Clamp01(y); // If the y axis is forward/back then minus is backwards, this will clamp the value between 0-1 so you can only go forward, not backward.
    5.  
    6. myTrans.forward = new Vector(x, y, 0 );
     
    Alimaestro likes this.
  3. Eli_VR23

    Eli_VR23

    Joined:
    Mar 11, 2023
    Posts:
    1
    what does mytrans mean?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,222
    I think it was supposed to be the transform.
    myTrans was just an example variable name.