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 Kacer · Sep 27, 2011 at 01:51 PM · movementphysicsvelocitylimitmagnitude

Constraining maximum movement speed on a rigidbody on certain axes.

Hello

I'm trying to create a game where ive got a character flying through some streets, i've got a problem though.

I've written some code that uses the "rigidbody.velocity.magnitude" to limit the maximum speed, as i dont want to be able to accelerate for infinity.

     if(rigidbody.velocity.magnitude < maxspeed){
         rigidbody.AddForce(transform.rotation * Vector3.up * 1);
         rigidbody.AddForce(transform.rotation * Vector3.right * horizontal);
         rigidbody.AddForce(transform.rotation * Vector3.forward * -vertical);
     }

(because i've turned my object, up is forward, and forward is vertical, yesyes, silly me :P)

The problem with this is, my forward momentum gets almost nullified when im moving to the sides, because my velocity is exceeding the maximum allowed.

I have also tried moving the first line outside the if-sentence, but that will give me some jaggy movement when im trying to move to the sides, again, because my movement is limited by my maximum velocity.

So, im curious, is there an alternative to what im doing?, if im messing up here because i've missed something in the documentation, then please point me in the right way.

Thanks in advance.

Kacer

Edit: here's the solution to my issue, on second thought it could probably have been done a bit simpler.

         if(rigidbody.velocity.z < maxforwardspeed)
             rigidbody.AddForce(transform.rotation * Vector3.up * 1);
         
         if(rigidbody.velocity.x > minspeed && rigidbody.velocity.x < maxspeed && rigidbody.velocity.y > minspeed && rigidbody.velocity.y < maxspeed){
             rigidbody.AddForce(transform.rotation * Vector3.right * horizontal);
             rigidbody.AddForce(transform.rotation * Vector3.forward * -vertical);
         }
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 yeoldesnake 1 · Sep 27, 2011 at 02:37 PM

You can try

rigidbody.AddForce(transform.rotation * Vector3.up * 1); rigidbody.AddForce(transform.rotation * Vector3.right * horizontal); rigidbody.AddForce(transform.rotation * Vector3.forward * -vertical);

if(Mathf.Abs(rigidbody.velocity.y)>maxspeed) rigidbody.velocity.y=maxspeed; if(Mathf.Abs(rigidbody.velocity.x)>maxspeed) rigidbody.velocity.x=maxspeed; if(Mathf.Abs(rigidbody.velocity.z)>maxspeed) rigidbody.velocity.z=maxspeed;

Comment
Add comment · Show 7 · 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 Kacer · Sep 28, 2011 at 08:36 AM 0
Share

This would not work, for the same reasons as above, your script will limit the total magnitude, which means, that if i max out my speed on the z axis, then i wont be able to move on the x and y axis.

also because rigidbody.velocity.magnitude is read only :)

avatar image yeoldesnake 1 · Sep 28, 2011 at 08:38 AM 0
Share

Thought so , then why not limit rigidbody.velocity.y , rigidbody.velocity.x and rigidbody.velocity.z

avatar image Kacer · Sep 28, 2011 at 08:51 AM 0
Share

Hmm, that works, though, i have to write some extra code, i'll write what i've written as an edit in my original post.

avatar image yeoldesnake 1 · Sep 28, 2011 at 08:52 AM 0
Share

I edited my post to include it as well

avatar image Kacer · Sep 28, 2011 at 08:54 AM 0
Share

well, your edit wont work, as you cant edit those values directly, plus you're not taking into account that when you're moving to the left you have a negative value as your velocity.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help with my movement script.. :( 1 Answer

(RigidBody) Velocity at an Angle 0 Answers

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

Adding player velocity to projectile not working 0 Answers

How do I check for movement? 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