- Home /
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;
}
}
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?
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?
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.
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.
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.
@clunk you put so much time in to this amazing you didn't get more votes
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.
Answer by Fattie · Dec 29, 2012 at 08:51 AM
Here's how to do it !
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!
I cant even tell you how excited this post made me even though i dont need this. Great idea and well written.
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 :-)
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 :)
Hi @sacred ! this is unrelated to colliders. you do not use an colliders here. It is a kinematic solution.
Does anyone have any thoughts on the best way to get the center line of the tube?
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
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
Follow this Question
Related Questions
How can i roll sphere/Ball having rigidbody with Rigidbody properties 0 Answers
Turning a rigidbody that's in motion 2 Answers
Non-rotating collider fixed to a rolling ball 1 Answer
How to make a RigidBody not go into ground when tilted foward? 2 Answers
How to get a ball with physics2d to move instead of spin? 1 Answer