- Home /
Can you drive a rigidbody
I an wanting to drive a rigidbody that doesn't have a character controller on it. I can make it rotate and I can have it pushed over time but I want to have the force asserted on my rigidbody only on a "key down." Does anyone know how to do that? I need the object to be able to move both forward and reverse. Thanks for any help.
Answer by Thom Denick · Feb 22, 2011 at 08:13 PM
This is a fairly simple scripting task. Here's an idea of what you need to do:
FixedUpdate() {
if (Input.GetKeyDown(KeyCode.UpArrow))
print("up arrow key is held down");
rigidbody.AddForce(Vector3.up * 10);
} else if (Input.GetKeyDown(KeyCode.DownArrow))
print("down arrow key is held down");
rigidbody.AddForce(-Vector3.up * 10);
}
Obviously, you need to Change AddForce to more accurately represent how much force you want to add with each push of the button. The force will only be applied once for each key push.
All of this stuff is easily found in the Script Reference, you definitely need to learn to use this:
GetKey: http://unity3d.com/support/documentation/ScriptReference/Input.GetKey.html
AddForce: http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html
Vector3.up: http://unity3d.com/support/documentation/ScriptReference/Vector3-up.html?from=Rigidbody
Your answer
Follow this Question
Related Questions
Top Down car/vehicle movement 1 Answer
how get bow to add more force over time 0 Answers
How to make a ball capable of rolling around a loop the loop? 1 Answer
Rigid body rotation question 1 Answer