• 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 peon · Aug 11, 2013 at 12:22 PM · movementrigidbodytopdown

Diagonally movement too fast (top-down)

Hi,

I'm making a top-down shooter to learn Unity. My character moves with rigidbody and I'm trying to limit (normalize?) the diagonally movement speed. How am I suppose to do it with this script:

 //Left
 
 if (Input.GetKey ("a")){
 
     rigidbody.AddForce(Vector3.right * -moveSpeed * Time.deltaTime);
     }
 //Right
 if(Input.GetKey("d")){
 
     rigidbody.AddForce(Vector3.right * moveSpeed * Time.deltaTime);
     }
 //Up
 
 if (Input.GetKey ("w")){
 
     rigidbody.AddForce(Vector3.forward * moveSpeed * Time.deltaTime);
     
     }
 //Down
 
 if(Input.GetKey ("s")){
 
     rigidbody.AddForce(Vector3.forward * -moveSpeed * Time.deltaTime);
 }
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

2 Replies

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

Answer by Elroyman · Aug 11, 2013 at 03:24 PM

The problem is when two buttons are being pressed you end up with a vector something like (1,1,0), which is longer that the single button options which will have components something like (1,0,0). One way to do this is to let the buttons change your direction vector then add the force separately. Try:

 Vector3 direction = new Vector3(0,0,0);
 //Left
 
 if (Input.GetKey ("a")){
     direction -= Vector3.right;
     }
 
 //Right if(Input.GetKey("d")){
    direction += Vector3.right;
     }
 
 //Up
 
 if (Input.GetKey ("w")){
     direction += Vector3.forward;     
     }
 
 //Down
 
 if(Input.GetKey ("s")){
     direction -= Vector3.forward;
 }
 if (direction != Vector3(0,0,0)
       rigidbody.AddForce(direction.normalized*moveSpeed*Time.deltaTime;

That will let you set the direction with each keypress, and then if the vector is not zero it will add the force in the specified direction. The '.normalized' specifies that no matter the direction, its magnitude will alsways be 1. Should keep your speed constant.

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 peon · Aug 11, 2013 at 04:30 PM 0
Share

That worked! Thanks a ton!

avatar image
0

Answer by bubzy · Aug 11, 2013 at 02:25 PM

if its too fast

try adjusting the value of moveSpeed

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 peon · Aug 11, 2013 at 03:27 PM 0
Share

The problem is that my character moves faster diagonally than to just one direction. I'm assu$$anonymous$$g it's because when two keys are pressed at the same time it adds moveSpeed + moveSpeed.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer

How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers

Pushing rigidbodies around 1 Answer

Touch input to move rigidbody using explosion force in C# 0 Answers

Prevent Rigidbody From Rotating 3 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