• 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 easilyBaffled · Dec 27, 2012 at 01:23 AM · rotationrigidbodyballturning

How to roll a ball along a curved tube.

I am developing a slalom game. I have a ball rolling over a tube, it can rotate all around the edge of the tube. So far the ball will constantly roll forward. The problem is that at some point there will be a turn in the tube I have no clue how to have the ball turn with the turn section. I can't just do look at because it won't smoothly turn and the turn piece can be at any angle. The control script is below and has its own problems here: http://answers.unity3d.com/questions/369543/quirky-unasked-for-rotation-with-ridigidbodies.html I've also included a picture of the turn, in this case the ball will be rolling down and to the right. Any ideas?

 var rot;
 var tube : Transform;
 function Start(){
     rigidbody.AddForce(Vector3.down*9.8);
 }
 function FixedUpdate () {
     rot = Input.GetAxis("Horizontal") * -10;
     if(rot != null && tube != null){
         Debug.Log("working");
         transform.RotateAround(tube.position, Vector3.forward, rot);
     }
     else{
         //rigidbody.AddForce(Vector3.forward*20);
         rigidbody.AddTorque (Vector3.right * 20);
     }
     rigidbody.AddForce((tube.position - transform.position) * 5 * Time.smoothDeltaTime);
     
     
 }
 function OnCollisionEnter(collision : Collision){
     if(collision.transform.tag == "tube"){
         tube = collision.transform;
     }
 }

alt text

screen shot 2012-12-26 at 8.22.24 pm.png (37.7 kB)
Comment
Add comment · Show 20
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 sacredgeometry · Dec 28, 2012 at 07:19 AM 0
Share

Could you make an animation along a curve thats placed in the centre of the tube in your 3d software then write a script that sets where along the animation the gravitational force is based on the balls relative position?

avatar image easilyBaffled · Dec 28, 2012 at 02:35 PM 0
Share

I'm sorry but I just barely scraped this together in $$anonymous$$aya, I have no clue how to make an animation, let alone do any of the rest that you said, could you perhaps dumb it down for me please?

avatar image clunk47 · Dec 28, 2012 at 05:12 PM 2
Share

Check out my pinball project. I had this exact need for one of my tables. Watch the video HERE. Watch for the first 5-10 seconds and you'll see my ball traveling through the metal "tube". How I did this, I placed waypoints inside my tube, and an "enter" and "exit" cube at each end of the tube, which are both triggers. When the 1st trigger is hit, the ball's rigidbody is set to kinematic, and a script kicks in that iterates through my waypoints, and uses Vector3.$$anonymous$$oveTowards to travel to each target waypoint. Once the "exit" trigger is hit, the ball's rigidbody is switched back to non-kinematic. Let me know if this is what you're looking for, and I'll dig up the old project from my backup drive and strip down the code a bit to fit your needs.

avatar image clunk47 clunk47 · Dec 30, 2012 at 11:36 PM 1
Share

I had it become kinematic, because I'm not using physics while the ball is in the tube. If I have physics enabled for the ball while it's going through the tube, the ball would try to use gravity and would not travel correctly. Once the ball hits the "exit" trigger, physics are re-enabled so I can play again like normal.

avatar image clunk47 clunk47 · Dec 30, 2012 at 11:38 PM 1
Share

And yes, this would work for your tube, so long as you place enough waypoints. The close the waypoints are together, the more accurate this will be. If you're going to have a bunch of waypoints, you could write up a script that will automatically place them throughout the tube.

avatar image Fattie clunk47 · Jan 01, 2013 at 10:23 AM 1
Share

@clunk you put so much time in to this amazing you didn't get more votes

Show more comments
avatar image sacredgeometry · Dec 28, 2012 at 11:27 PM 0
Share

in that case how about just making it stay certain distance from the surface using the mesh and surface normals as a guide for rotation.

avatar image AlucardJay · Dec 29, 2012 at 11:02 PM 1
Share

http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html

Show more comments

2 Replies

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

Answer by Fattie · Dec 29, 2012 at 08:51 AM

Here's how to do it !

alt text

Notice the "coin" ...

The "coin" would be invisible. But make it visible at first, so you can see what you are doing.

The "coin" would be the same diameter as the tube. But at first make it 10% bigger so you can see it clearly. It will peek out of the tube.

Notice the "coin" has the small ball attached to it.

Notice the "FORWARD" direction (arrow) of the "coin".

As the "coin" moves through the tube:

The FORWARD direction must always match the forwards direction in the tube at any differential - in other words, the tangent of the centerline at any point.

(Indeed - this is how you define a tube! By sweeping out a surface using a circle.)

To make the user steer "left and right" just rotate the coin (of course, rotate it around it's FORWARD axis).

Code requirements: need the centerline of the tube. Need a line of code that moves the coin along the centerline. Need a line of code that sets the FORWARD to the current tube tangent.

So that's probably the easiest way to do it! This is flawlessly mechanical - recall that the way you produce a tube is by sweeping out a circle in space - so the SmallBall will always be exactly on the surface. Happy new year!


coinsweepsouttube.jpg (184.4 kB)
Comment
Add comment · Show 8 · 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 Lovrenc · Jan 01, 2013 at 10:12 AM 2
Share

I cant even tell you how excited this post made me even though i dont need this. Great idea and well written.

avatar image Fattie · Jan 01, 2013 at 10:39 AM 0
Share

BTW the real beauty here: see the larger drawing of the coin with the ball. imagine you add a line of code to make the ball rotate correct ("rolling forwards" if you will) as seen in the larger drawing.

Then - incredibly - as you rotate the control coin, it magically makes the rotation of the ball work perfectly 'on' the surface of the tube.

Otherwise it's an nightmare to make it roll properly considering other movement, etc.

Furthermore!!!! Regarding the roll of the white ball "left and right". You trivially add a line of code that makes it rotate (simply based on it's diameter) locked to the rotation of the "coin" - it's that easy. The whole thing is sort of miraculous.

I love stuff like that where you don't have to do any work :-)

avatar image sacredgeometry · Jan 01, 2013 at 12:35 PM 0
Share

Interesting solution I am not sure how reliable it would be if you are relying on colliders to feed the coin down the tube but worth a try :)

avatar image Fattie · Jan 01, 2013 at 02:07 PM 0
Share

Hi @sacred ! this is unrelated to colliders. you do not use an colliders here. It is a kinematic solution.

avatar image tgate · Jan 08, 2013 at 10:17 PM 0
Share

Does anyone have any thoughts on the best way to get the center line of the tube?

Show more comments
avatar image
1

Answer by ChrisBliss · Dec 28, 2012 at 01:33 AM

Can you read out the normal of your collisionpoint and apply a force in the opposite direction? then it should alway stick to the surface instead of rotating around the tube. your asset looks fairly 'highpoly' so it should look smooth enough

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 easilyBaffled · Dec 29, 2012 at 10:51 PM 0
Share

Like I said to sacredgeometry, I am not sure how to access the normal then get its direction. I imagine its something like: access collision/raycast point, access point normal, find its "direction"/perpendicular to the ground. but its really that third part i cant really figure out

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

16 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

Related Questions

Non-rotating collider fixed to a rolling ball 1 Answer

How can i roll sphere/Ball having rigidbody with Rigidbody properties 0 Answers

Turning a rigidbody that's in motion 2 Answers

Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers

Pysics not working as expected 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