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 /
This question was closed Mar 07, 2015 at 04:17 PM by FatalVitality for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by FatalVitality · Feb 17, 2014 at 11:42 PM · c#2dmovementplatformer

(Solved) Continuous movement

Hi, I am trying to get my player to move in one direction continuously and then move the opposite direction continuously when I press a button. So far I've been able to get the player to move in one direction continuously, however when I want it to go the other way I have to hold down a button rather than just press it once.

This is what I've got (btw this is for a 2D game)

 rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);
 
 bool movementKeyPressed = false;
 
 if (Input.GetKey(KeyCode.RightArrow)|| Input.GetKey(KeyCode.D))
 
 {rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);
 movementKeyPressed = true;
 }
 
 if (Input.GetKey(KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A))
 
 {
 rigidbody2D.velocity = new Vector2(-transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);    
 movementKeyPressed = true;
         }
Comment
Add comment · Show 3
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 Maui-M · Feb 17, 2014 at 11:54 PM 0
Share

$$anonymous$$ake sure this line isn't in your update function

 rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);

Also, it would help if your code in your post was formatted better.

avatar image FatalVitality · Feb 18, 2014 at 12:04 AM 0
Share

Where would I put it ins$$anonymous$$d?

avatar image Eric5h5 · Feb 18, 2014 at 08:50 PM 0
Share

Accept the answer if it was helpful; don't write "solved" in the title.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by thaiscorpion · Feb 18, 2014 at 12:12 AM

You can try keeping this line:

 rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);

But add in a direction variable, that would be '1' if going right and '-1' if going left and multiply it like this:

 rigidbody2D.velocity = new Vector2(playerDirection * transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);

Now if you change you input ifs like this:

 if (Input.GetKey(KeyCode.RightArrow)|| Input.GetKey(KeyCode.D)){
  
 playerDirection = 1;
 movementKeyPressed = true;
 }
  
 if (Input.GetKey(KeyCode.LeftArrow)|| Input.GetKey(KeyCode.A)){
  
 playerDirection = -1;
 movementKeyPressed = true;
 }

This way once you press the button the player will continue to move in that direction until you press another direction button. Hope that helps.

Comment
Add comment · Show 3 · 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 FatalVitality · Feb 18, 2014 at 12:25 AM 0
Share

Thanks thaiscorpion, that works; but when creating a variable was I supposed to write

int playerDirection = 1;

avatar image thaiscorpion · Feb 18, 2014 at 09:20 AM 0
Share

yes sry I didnt create the variable :D

avatar image FatalVitality · Feb 18, 2014 at 09:46 AM 0
Share

Thanks man, hats off for you :D

Follow this Question

Answers Answers and Comments

22 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

Related Questions

animation 2d platformer 2 Answers

Why do I double jump? This isn't supposed to happen... 2 Answers

2d platformer dash move. 1 Answer

Animating 2D sprite upon movement script help. 2 Answers

Damaging Enemies 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