Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CHPessolani · Jul 21, 2015 at 03:56 PM · 2dmovementinputdisabletron

Prevent player from moving in a certain direction while moving in another

Hi. I am making a Tron light-cycle game and I having some trouble figuring how to stop the player from moving on the opossite direction that he is already moving. Right now I am creating a colored square on each change of direction and between them I create the light wall, the problem occurs when I am moving in a single direction since I just have one colored square of the two I need to create a solid wall with fitColliderBetween, the light wall behind the player is not solid yet and the player can move freely up and down the wall until he hits the last colored square used to initiate the fitColliderBetween or until he moves again in the direction he was facing initially and hit the square created when he turned in the opossite direction that he was moving. This can be prevented by disabling the input for moving in the undesired direction. If the player is moving UP, he should not move DOWN but he can choose to move RIGHT or LEFT.

If there a way to disable the player input for a single direction? In this case the opposite one. I already have a way of telling which way the player is facing (used to spawn projectiles) but I do not know how to disable a single input, I found similar answers but they wanted to disable all player input while keeping the player's momentum and I just want to disable only one direction.

 public enum PlayerFacing
 {UP, DOWN, LEFT, RIGHT}

 public PlayerFacing facing;


 // Use this for initialization
 void Start () {
     //Initial velocity so it starts moving
     GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
     spawnWall ();

     
 }


 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown (upKey)) {
         // Do is key pressed
         GetComponent<Rigidbody2D> ().velocity = Vector2.up * speed;
         //Spawn wall
         spawnWall ();
         facing = PlayerFacing.UP;
     } else if (Input.GetKeyDown (downKey)) {
         GetComponent<Rigidbody2D> ().velocity = -Vector2.up * speed;
         spawnWall ();
         facing = PlayerFacing.DOWN;
     } else if (Input.GetKeyDown(rightKey)) {
         GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
         spawnWall ();
         facing = PlayerFacing.RIGHT;
     } else if (Input.GetKeyDown (leftKey)) {
         GetComponent<Rigidbody2D> ().velocity = -Vector2.right * speed;
         spawnWall ();
         facing = PlayerFacing.LEFT;
     }else if (Input.GetKeyDown (shotKey) && Time.time > nextFire) {
             nextFire = Time.time + fireRate;
         //Instantiate (basicshot, bulletSpawn.position, Quaternion.identity);
         BasicShoot (basicshot, 150);
     } 

         // Fill between walls
         fitColliderBetween (wall, lastWallEnd, transform.position);
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by PouletFrit · Jul 21, 2015 at 04:45 PM

Just add a variable to keep track of your direction and add a condition in your input verification

 // Create an enum and simply cache your direction in a variable
 public enum Direction {
     Left, Right, Up, Down
 }
 public Direction currentDirection;

 // And then update currentDirection appropriatly in update and check it
 if (Input.GetKeyDown(upKey) && currentDirection != Direction.Down) {
     // your movement code here
     currentDirection = Direction.Up;
 } else if ...


also i suggest you to cache your rigidbody2D in a variable instead of refetching it in every update.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPessolani · Jul 22, 2015 at 01:35 PM 0
Share

Thanks a lot. It works perfectly. I was so focused on disabling input that I could not come with any other way of doing it.

Since the player is always moving, PlayerFacing already has the current direction so it is easy to add the extra code. Thanks a lot.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Prevent Player from moving into opposite direction than he already is 1 Answer

My controls are inverted when facing sideways. 1 Answer

How can I stop the player movement from being too fast while moving diagonally in my 2D game? 2 Answers

2D Top-Down Movement: Transform.up and right don't change 2 Answers

NullRefrenceException with new input system 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges