• 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 /
  • Help Room /
avatar image
Question by LimitedLack · Sep 22, 2020 at 03:39 AM · rigidbodyvector3rigidbody.addforcerigidbody.velocityvectors

Only limit player input and not external forces (Rigidbody 3D)

Hey! I have been trying to properly limit movement on my 3D fps controller, and I have so far just been using basic clamping to limit the speed on the X and Z axis. The problem with this is that any external forces will never cause the player to exceed its predefined maximum speed, meaning that external forces like explosions do basically nothing to move the player like an explosion should. My idea for getting this to work was to only allow additional movement input if the player was below a certain maximum velocity, but so far I have been unable to get it to work in code. I am fairly new to working in 3D space with vectors, so I am pretty lost on what to do. My attempted implementation worked until the player turned their head while moving, at which point the movement would not be limited. Here is the code I have tried:

 xInput = Input.GetAxisRaw("Horizontal");
 yInput = Input.GetAxisRaw("Vertical");
     
 if (xInput > 0 && transform.InverseTransformDirection(rb.velocity).x > inputMaximumSpeed) xInput = 0;
 if (xInput < 0 && transform.InverseTransformDirection(rb.velocity).x > -inputMaximumSpeed) xInput = 0;
 if (yInput > 0 && transform.InverseTransformDirection(rb.velocity).z > inputMaximumSpeed) yInput = 0;
 if (yInput < 0 && transform.InverseTransformDirection(rb.velocity).z > -inputMaximumSpeed) yInput = 0;
     
 //Apply Movement
 moveDirection = (xInput * transform.right + yInput * transform.forward).normalized;
 rb.AddForce(moveDirection * groundAccSpeed);

Not too sure what to do to fix this, as I have found little to none in terms of examples on how others have gotten this to work. Thanks for any help!

Comment

People who like this

0 Show 0
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

Answer by streeetwalker · Sep 22, 2020 at 08:36 AM

you can AddForce multiple times over the course of a FixedUpdate frame, and the physics engine will aggregate the forces together for you. To limit the force due to input, calculate the resultant velocity due to the input force manually before you add the force. If you are over the limit, you can set the input force vector to zero.

Another method is to accumulate your forces just by adding all the force vectors together - that is what Unity is doing if you use AddForce multiple times. Then you can use AddForce once. Again, you want to limit the force due to input and you use the same method.

You don't show us enough code to understand what you mean by your statement about velocity not being limited when you look to the side.

Comment

People who like this

0 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 LimitedLack · Sep 22, 2020 at 10:18 PM 0
Share

I managed to get it to kind of work by changing the code to this:

 xInput = Input.GetAxisRaw("Horizontal");
 yInput = Input.GetAxisRaw("Vertical");
 
 //Apply Movement
 moveDirection = (xInput * transform.right + yInput * transform.forward).normalized;
 
 if (rb.velocity.magnitude < inputMaximumSpeed)
     rb.AddForce(moveDirection * groundAccSpeed);

This does limit the movement by not allowing further inputs if they will cause the player to go over the maximum speed while still allowing external forces, as well as fixing the issue that caused the player to go faster while turning (as long as I have the counter-movement portion of the code running), but the new issue is that it also prevents any turning at all when rotating the player. I've been looking for ways to fix this for a couple of hours, but I can not find or come up with a way to fix it. Not sure if this is the method you intended on how to limit the speed though, I might have misunderstood what you meant.

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

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

how to change the velocity of a rigid body based on a forward 0 Answers

Player rigidbody jumping continues infinitely 1 Answer

revolving a rigidbody around another MOVING rigidbody 0 Answers

Accelerate Player with Rigidbody.AddForce 0 Answers

Speed limit to AddForce (without limiting velocity of rigidbody) 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