Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Laran · Nov 28, 2013 at 06:18 PM · c#player movement

Moving in one direction at the time

Hello.

I would like to move my character only in one direction at the time (only left, or only top etc). It works correctly but the problem is that when I move horizontally I can go up and down (by pressing W or S) while still pressing A or D (which is OK), but when I move vertically I have to stop pressing W or S to move left or right. I know why this is happening, but I don't know how to fix that. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     //Public variable to change speed of the player
     public float MovementSpeed = 20f;
 
     void Start () {
     
     }
 
     void Update () {
 
         float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
         float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
 
 
         //This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
         bool statusHorizontal = Input.GetButton("Horizontal");
         bool statusVertical = Input.GetButton("Vertical");
 
         //By default character is not moving
         Vector3 speed = new Vector3 (0, 0, 0);
 
         //This is stupid, but in original Tanks 1990 player was able to move forward OR to the side
         if (statusVertical == true)
         {
             speed.z += forwardSpeed;
         }
         else if (statusHorizontal == true)
         {
             speed.x += sideSpeed;
         }
         
         
         CharacterController cc = GetComponent<CharacterController> ();
 
         cc.SimpleMove (speed);
 
     }
 }
 




Yes, I'm going something a 'la Tanks 1990 to learn Unity.

~Laran

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
0
Best Answer

Answer by robertbu · Nov 28, 2013 at 08:06 PM

Figure out this kind of logic can get ugly. I think this is what you are trying to do:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController1 : MonoBehaviour {
     
     //Public variable to change speed of the player
     public float MovementSpeed = 20f;
 
     private bool prevStatusVertical;
     private bool prevStatusHorizontal;
     private bool moveHorizontal;
 
     private CharacterController cc;
     
     void Start () {
         cc = GetComponent<CharacterController> ();
     }
     
     void Update () {
         
         float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
         float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
         
         
         //This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
         bool statusHorizontal = Input.GetButton("Horizontal");
         bool statusVertical = Input.GetButton("Vertical");
 
         if (statusHorizontal && !prevStatusHorizontal) 
             moveHorizontal = true;
         if (statusVertical && !prevStatusVertical || !statusHorizontal)
             moveHorizontal = false;
         
         //By default character is not moving
         Vector3 speed = new Vector3 (0, 0, 0);
         
         if (statusVertical && !moveHorizontal)
         {
             speed.z += forwardSpeed;
         }
         else if (statusHorizontal)
         {
             speed.x += sideSpeed;
         }
 
         prevStatusVertical = statusVertical;
         prevStatusHorizontal = statusHorizontal;
         
         cc.SimpleMove (speed);
     }
 }
Comment
Add comment · Show 5 · 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 Laran · Nov 29, 2013 at 07:32 PM 0
Share

Thank you, but this still don't satisfy me. Problem is that when I press A or D (Horizontal), then W or S (Vertical), then release Vertical (but still keep pressing Horizontal) my character is NOT moving. It works however when I "press Vertical > press Horizontal > release Horizontal"

Tell me if I explained this badly:P

"$$anonymous$$e no Englando" :P

avatar image robertbu · Nov 29, 2013 at 07:50 PM 0
Share

I added a $$anonymous$$or tweak that fixes the issue you outline.

avatar image Laran · Nov 29, 2013 at 08:37 PM 0
Share

Thank you, but I have one more (silly) question.

In line 29 there is: "If variable1 is true and variable2 (which is equal to variable1) is false, do something"

I don't see any logic in this. For me it's like "if sun shines and sun does not shine, do something..."

avatar image robertbu · Nov 29, 2013 at 09:15 PM 0
Share

Take a close look at the variable names. On is 'statusHorizontal' the second one is 'prevStatusHorizontal'. This code is testing for the change is state of statusHorizontal. So if in the previous frame statusHorizontal was false, and in the current frame it is true, then we set 'moveHorizontal' to true.

avatar image Laran · Nov 29, 2013 at 09:51 PM 0
Share

Oh wait... I completely forgot that this whole code is executed in every frame (so prevStatusVertical has value of previous frame, not current).

This make sense now. You are great! (and I have a lot to learn :P)

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# -- Build character unit from script 1 Answer

C# Input.GetKey("Tab") Double Tap 1 Answer

C# Throw GameObject Upon Mouse Click 1 Answer

Player lives script help 1 Answer

Null in GetValidMethodInfo 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