Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Alsador · Oct 18, 2015 at 09:47 PM · movementplayervelocityarrow-keys

Velocity based movement (2D)

So I came across an issue with my movement for my character in this game. The left control works perfectly but when use the right arrow key nothing happens. When i remove the code for the left key the right key works. When i place the left key code above the right key code neither of them work. Here is the code.` public float speed = 8f; public float upSpeed = 5f; public Rigidbody2D player;

 // Use this for initialization
 void Start () {
 
     player = GetComponent<Rigidbody2D> ();

 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetKey (KeyCode.RightArrow)) {
         player.velocity = new Vector2 (speed, 0);
     } 
     else {
      Input.GetKeyUp(KeyCode.RightArrow);
             player.velocity = new Vector2(0, 0);
     }

     if (Input.GetKey (KeyCode.LeftArrow)) {
         player.velocity = new Vector2 (-speed , 0);
     } 
     else {
         Input.GetKeyUp(KeyCode.LeftArrow);
         player.velocity = new Vector2(0, 0);
     }
 
     float upMove = Time.deltaTime * 5;

     transform.Translate(0, upMove, 0, Space.World);


 }
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
2
Best Answer

Answer by Eno-Khaon · Oct 18, 2015 at 10:06 PM

Okay, this is a little bit of a mess right now, so first off, here's what's happening with your code at the moment:

First, you check (per frame) whether the right arrow key is actively held. If it is, you move right.

However, if you're not holding right, your formatting is wrong. You're using

 else {
       Input.GetKeyUp(KeyCode.RightArrow);
 }

when, logically, you would want something more like

 else if(Input.GetKeyUp(KeyCode.RightArrow)) {
 // ...
 }

The same problem applies on the second round with the left arrow key.

However, there's an even simpler approach to all of this in the first place. If you make use of Unity's control systems, you can use GetAxis() (or GetAxisRaw()) to define your control scheme in fewer terms with reasonable accuracy.

To give an example of this:

 void Update () {
 
      float inputHorizontal = Input.GetAxisRaw("Horizontal");
 
      player.velocity = Vector2.right * inputHorizontal * speed;
      // ...
 }

In this manner, your inputs are controlled by left and right (or, for that matter, A and D in the WASD control scheme) and are theoretically customizable. Additionally, it automatically balances out the input back to 0 if you hold both to the left and right simultaneously.

Edit: That said, I would generally recommend against directly setting the velocity vector directly, and would instead suggest utilizing AddForce(), as per the typical recommendations. Doing so involves a fair bit more work, however, but can result in very clean gameplay.

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 Alsador · Oct 18, 2015 at 10:44 PM 0
Share

Thanks a lot. I appreciate this, I'm still really new to Unity and C#(coding in general too), I used the Input.GetAxisRaw("Horizontal") but the positive and negative buttons seem to be inverted (Right moves it left, Left moves it right). I checked my input manager to see if everything was right and it was. Right was set as positive and left set as negative.

EDIT- Ins$$anonymous$$d of Vector2.right i used Vector2.Left and it seems to be working exactly how I want it!

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

32 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 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

Return Proper Velocity of Player object 3 Answers

Rigidbody player movement unrealistic velocity problem. 1 Answer

Stopping an object immediately 1 Answer

Slowing Down When Sliding Across Walls 2 Answers

Player Going through colliders 1 Answer


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