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 /
This question was closed Nov 10, 2018 at 03:06 PM by PandrPi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by PandrPi · Nov 06, 2018 at 09:08 AM · vector3gravityclamp

How to limit a movement vector without affecting gravity

Hello, I wrote a script to control my spaceship, and I can limit its speed by using Vector3.ClampMagnitude(myRigidbody.velocity). However it affects a gravity and if I move down to the planet's surface my max speed is limited and it is not realistic. My question is how limit my movement vector( I use AddForce to apply it to my rigidbody) but keep gravity vector, which I also apply to rigidbody by using AddForce method, unlimited. Thanks in advance

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

  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Nov 09, 2018 at 12:41 PM

If your gravity is not "world space" down and you only want to clamp the tangent velocity you can seperate the velocity into the "vertical" / gravity part and the "horizontal" / tangential part.


Of course to do this you have to provide the direction of gravity as a vector for reference.

 public Vector3 ClampVelocity(Vector3 aVelocity, Vector3 aGravity, float aMaxTangentialVelocity)
 {
     var grav = Vector3.Project(aVelocity, aGravity);
     var tan = aVelocity - grav;
     tan = Vector3.ClampMagnitude(tan, aMaxTangentialVelocity);
     return tan + grav;
 }

This method basically works the same as "ClampMagnitude" but it removes the velocity part along the gravity direction, clamps the remaining part and adds the gravity part back at the end.

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 PandrPi · Nov 10, 2018 at 03:06 PM 0
Share

Thank you a lot. The only thing I had to change is to use own Project method.

 public Vector3 Project(Vector3 vector, Vector3 onNormal) 
 { 
 Vector3 vector3; 
 float single = Vector3.Dot(onNormal, onNormal); 
 float dot = Vector3.Dot(vector, onNormal); 
 vector3 = (single >= $$anonymous$$athf.Epsilon ? (onNormal * ((dot >= 0f) ? dot : 0f)) / single : Vector3.zero); 
 return vector3; 
 } 

Thus I can also limit a velocity when angle between velocity and gravity direction is more than 90 degrees. And this is what a need.

avatar image
1

Answer by swanijam · Nov 06, 2018 at 03:41 PM

You could try limiting the velocity only in non-downward directions.

try this (untested)

  Rigidibody rb;
 bool separatedDownardVector = false;
 Vector3 tempVelocity = rb.velocity;
  // removes the y component if the object is moving downward
  if (tempVelocity.y < 0f) {
      separatedDownardVector = true;
      tempVelocity.y = 0f; 
  }
  // clamp the magnitude of the edited velocity.
  tempVelocity = tempVelocity.normalized * Mathf.Clamp(0f, maximumSpeed, tempVelocity.magnitude);
  // adds the downard vector back, if it was removed earlier.
  if (separatedDownardVector) tempVelocity += rb.velocity.y;
  rb.velocity = tempVelocity; 

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 PandrPi · Nov 06, 2018 at 11:24 PM 0
Share

Thank you, but my gravity is not directed to vector3.down(vertically down), I have a planet gravity, so I need to remove some vector from velocity but not only y component. Some ideas?

Follow this Question

Answers Answers and Comments

121 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

Related Questions

object transform and clamp in local direction 0 Answers

Distance between 2 objects exteriors? 1 Answer

Limit Camera movement by distance from target 1 Answer

Vector3 is always subject to serious stutter in Translation 0 Answers

Applying force with respect to angle with the ground? 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