- Home /
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));
Answer by digitalConundrum · Mar 02, 2011 at 03:42 AM
Try this:
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html?from=ForceMode
with this:
http://unity3d.com/support/documentation/ScriptReference/ForceMode.Impulse.html
Answer by Joshua · Mar 02, 2011 at 03:53 AM
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. :)
Your answer
Follow this Question
Related Questions
Dragging object out of position 2 Answers
how to set initial speed of vehicle 1 Answer
Need help with this script 0 Answers
How to set velocity of Rigidbody without changing gravity? 1 Answer