- Home /
How to make a ball capable of rolling around a loop the loop?
Hello to all. In my game, I want the ball player object to go around a loop the loop like a hotwheels car. I knew that rb.addForce() wouldn't work, since it pushes the ball in one direction corresponding to the key pressed, therefore just ending up pressing against the wall halfway up. Instead, I tried rb.addTorque, but I ran into a problem with that as well, since the different axis turn as the ball does. I think I'll have to make it so that the axis of the ball stays relative to the world, therefore guaranteeing that it will spin in the desired direction, but I have no idea how. If anyone knows or has a better solution, I would be very grateful. Thanks!
O$$anonymous$$, if I got it right, and you want to make a sphere rotate like a wheel steering to left or right, then you will need some physics.
Firstly you have to define a rotation radius, let's call it R
Next you have to define a point around which the ball will rotate. Assu$$anonymous$$g you are moving forward, that point will be (x+R, y, z) when turning right, and (x-R, y, z) when turning left (supposing x, y, z are the coordinates of the ball).
Now you have to apply a radial acceleration. This type of acceleration doesn't modifies the velocity of an object, but rotates it, and it's defined as |a|=v²/R
This means that you should define an acceleration magnitude, something like float mag = rigidbody.velocity.sqr$$anonymous$$agnitude / R;
Now you need the acceleration direction, which will always go from the ball to the point. Your resulting acceleration should be
Vector3 acc = (point - transform.position);
acc = acc / acc.magnitude * mag;
Next apply that force to the Rigidbody. (keep in count that this will only work if your ball's mass is 1kg. Otherwise, just mutilpy the acceleration by the ball's mass (in kg)
Now, if you apply this in each fixed update, your ball will rotate to the left (or right), and it's velocity won't be changed (linear drag, friction and other external forces may change your velocity, they probably will).
When you want your ball to accelerate or deaccelerate, simply modify it's velocity before calculating the radial acceleration (as it depends on the object's velocity)
Answer by Happeloy · May 11, 2018 at 02:51 PM
You could possibly try adding a force in the direction the ball is currently moving, that should work. You can access that direction from rb.velocity. I guess you should normalize the vector, so the amount of force added doesn't grow with the current speed.
Something like
rb.AddForce(rb.velocity.normalized * force);
where force is a float with some value that gives you a good amount of push.
Though I have no idea if this will give you the behaviour you want, but it seems plausible, since the velocity vector will adjust an change each fixedupdate and should let you go around your loop. Though I don't think you should use this always, since it wont work if there is no velocity to start with.
That would just make sure the ball never stops moving, and that it keeps moving always on the last direction you ordered it to move