x


Rigidbody - Applying One-Time Force? Not Constant Force

Hey everybody!

I'm making a game where dice are rolled. Right now what I'm working on is having the dice be randomly thrown at the Start Function. How can I do this with a rigidbody? Everything I've tried so far applies a constant force. I need to apply both a random torque and velocity.

Right now what I've tried (it's constant though) is:

rigidbody.velocity = Vector3(Random.Range(-20.0, 20.0), 0, Random.Range(-20.0, 20.0));
rigidbody.AddTorque (Vector3.up * Random.Range(-10.0, 10.0));
more ▼

asked Mar 02 '11 at 03:31 AM

MythStrott gravatar image

MythStrott
176 22 23 31

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

2 answers: sort voted first

The way I would do it is using the rigidbody.velocity function like you did. Difference is, you probably stuck it inside your update or fixedupdate function meaning it'll happen thousands of times a second. Since you only want it executed once simply wrap it inside an if().

So on the top of your function add the variable:

var shouldIThrow : boolean = true;

Now inside your update or fixedupdate or whatever you use write:

if(shouldIThrow) { rigidbody.velocity = stuff you had; shouldIthrow = false; }

The added advantage is that now you can also set up a button or whatever you'll use to activate the dice with. If you make this button set the shouldIThrow variable to true they'll be thrown. :)

more ▼

answered Mar 02 '11 at 03:53 AM

Joshua gravatar image

Joshua
6.4k 19 25 70

(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:

x3446
x1783
x317
x47

asked: Mar 02 '11 at 03:31 AM

Seen: 2739 times

Last Updated: Mar 02 '11 at 03:31 AM