Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 KadenZombie8 · Jul 25, 2021 at 07:37 PM · rigidbodyangularvelocity

How can I stop a ball from rolling down while moving across a slope?

I have a simple rolling ball setup. However, I would like my ball to not roll down while moving along a slope's surface. For illustration: alt text

  • The green arrow shows the path the ball takes when it is not controlled by the user

  • The blue arrow shows the path that I want the ball to take when it is moved along the surface

  • The yellow arrow shows the approximate path that the ball actually takes when moved in the direction of the blue arrow


How can I get the ball to follow the blue path when it is strictly told to move in that direction? (I need this to work at many different angles so freezing the rigid body's rotation is out of the question)

I'm not sure if this will help much but here is the code to make the ball move:

 void FixedUpdate()
     {
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
         float mouseX = Input.GetAxis("Mouse X");
         float mouseY = Input.GetAxis("Mouse Y");
 
         cam.transform.Rotate(Vector3.right * -mouseY * Time.deltaTime * 500);
         player.transform.Rotate(Vector3.up * mouseX * Time.deltaTime * 500);
 
         if (Input.GetKey(KeyCode.LeftShift))
         {
             speed = 10;
         }
         else
         {
             speed = 5;
         }
 
        rigidbody.angularVelocity = (cam.transform.right * (vertical * speed)) + (player.transform.forward * (-horizontal * speed));
     }
ball.png (238.4 kB)
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
1
Best Answer

Answer by tyruji · Jul 26, 2021 at 06:17 PM

The reason why your ball is sliding down the slope is because of gravity. What you could do is stimulate your own gravity - you'd get the closest surface and its normal and then apply the gravity relative to it. Here some example code:

 private void ApplyGravity( float gravity_mag, float checkRadius, int layerMask )
 {
         Vector3 gravityDir;
         Vector3 pos = player.transform.position;
         Collider closest_col = null;
         float closest_dist = float.maxValue;
 
         foreach( var col in Physics.OverlapSphere( checkRadius, layerMask, 
                                                         QueryTriggerInteraction.Ignore) )
         {
                 float new_dist = ( col.transform.position-pos ).sqrMagnitude;
                 if( new_dist >= closest_dist ) continue;

                 closest_dist = new_dist;
                 closest_col = col;
         }                // sort through hit colliders and get the closest one
 
         if( closest_col == null ) //didn't hit anything, apply default gravity
         {
                 rigidBody.AddForce( Vector3.down*gravity_mag, ForceMode.Acceleration );
                 return;
         }
         
         // now raycast to get the surface's normal
         var ray_dir = ( closest_col.transform.position-pos ).normalized;

         Physics.Raycast( pos, ray_dir, out RaycastHit hit, checkRadius, layerMask, 
                                                         QueryTriggerInteraction.Ignore );

         rigidBody.AddForce( -hit.normal*gravity_mag, ForceMode.Acceleration );
         // and add force in the opposite direction of the surface's normal
 }

Sorry, if the code is unclear or has any mistakes, I'm not on a computer. Hope it gives you the idea, good luck!

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 KadenZombie8 · Jul 30, 2021 at 11:07 PM 0
Share

Thank you so much for helping me with this problem! My sphere now works as expected and I couldn't have done it without you. :)

For those interested, here is the new code for the locomotion sphere:

 //Get inputs
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
         float mouseX = Input.GetAxis("Mouse X");
         float mouseY = Input.GetAxis("Mouse Y");
 
         //Looking around
         cam.transform.Rotate(Vector3.right * -mouseY * Time.deltaTime * 500);
         player.transform.Rotate(Vector3.up * mouseX * Time.deltaTime * 500);
 
         //Running
         if (Input.GetKey(KeyCode.LeftShift))
         {
             speed = 10;
         }
         else
         {
             speed = 5;
         }
 
         //Rotate locosphere
         if(Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit, 0.25f))
         {
             rigidBody.AddForce(-hit.normal * 9.81f, ForceMode.Acceleration);
         }
         else
         {
             rigidBody.AddForce(Vector3.down * 9.81f, ForceMode.Acceleration);
         }
 
 
         rigidBody.angularVelocity = (cam.transform.right * (vertical * speed)) + (player.transform.forward * (-horizontal * speed));

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

171 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

Related Questions

minimum angular velocity? 1 Answer

Rigidbody angularvelocity shortest way 1 Answer

How to rotate an object to a specific angle with angularVelocity? 0 Answers

AddRelativeTorque results in incorrect angular acceleration (Maybe centrifugal force affects) 0 Answers

rigidbody has a max rotation velocity ? 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