- Home /
moving and jumping ball using physics
hi, I know how to move static objects, with transforms. but how can I move ball with physics. I mean I click forward button[w] and ball rolls forward following terrain. when I click jump[space] it jumps. also can I make ball bounce up and down all the time? I got this effect while messing with physics components and kind of liked it. (very relaxing to watch xD)
Answer by StephanK · Apr 28, 2010 at 10:00 PM
To make the ball bounce all the time set its physics material bounciness to 1 and the bounce combine to maximum. To "steer" the ball with physics you will need to either apply forces to its rigidbody or set its velocity directly. Both can be done with member functions of the rigidbody component, which you can look up in the scripting reference.
I would suggest using forces for steering and only use velocity for jumping or instantanious speed boosts.
Answer by Waz · May 01, 2010 at 10:35 PM
You'll probably want to apply torque to rotate the ball. Then its friction with the surface it is on will convert that into motion. This works when applied to a sphere:
function FixedUpdate() {
var torque = Vector3(Input.GetAxis("Vertical"), 0, -Input.GetAxis("Horizontal"));
torque = Camera.main.transform.TransformDirection(torque);
torque.y = 0;
rigidbody.AddTorque(torque.normalized*speed);
}
If you instead apply the force directly to the ball, it might seem to work similarly, because the friction with the contact surface will cause it to roll (the inverse of the above). However, on, say, ice, it will accelerate faster (and not roll much). Of course, that might be what your game requires.
Thanks Warwick! But I have a problem with this which is that the ball doesn't stop after applying the AddTorque until it reaches a collider. Given the friction and gravity, It's supposed to lose the energy after a while and stop, but it don't! Could you please help in this issue as well? Thanks.
Your answer
Follow this Question
Related Questions
How to roll a ball with moving platform. 1 Answer
rolling ball is a child that needs to be steered by parent 1 Answer
Ball rolling on platform 0 Answers
How to make a ball that can roll? 2 Answers