x


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)

more ▼

asked Apr 28 '10 at 07:11 PM

Hellwalker gravatar image

Hellwalker
35 7 7 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 28 '10 at 10:00 PM

StephanK gravatar image

StephanK
6k 39 53 93

I would suggest using forces for steering and only use velocity for jumping or instantanious speed boosts.

Apr 29 '10 at 05:35 AM Random Indie

Thx I got it working

Apr 29 '10 at 07:38 PM Hellwalker
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 01 '10 at 10:35 PM

Waz gravatar image

Waz
6.4k 22 33 71

Thanks bro!

May 02 '12 at 02:33 AM Chimera3D

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.

Oct 01 '12 at 04:12 PM ebad
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1877
x169
x24

asked: Apr 28 '10 at 07:11 PM

Seen: 4653 times

Last Updated: Oct 01 '12 at 04:12 PM