I am a bit confused about how rigidbody.AddForce works…after reading the script reference it is my understanding that once a rigidbody is attached to an object, i can use something like rigidbody.AddForce(0, 0, 10) to move the object forward. Im using something like this:
Update() {
if (Input.GetKey (“w”)) {
isMovingForward = true;
}
}
FixedUpdate() {
if (isMovingForward) {
rigidbody.AddForce (0, 0, speed * resistance);
}
}
This code is not moving my object, I’m obviously missing something, it is just unclear to me. my rigidbody has gravity checked, is not kinematic, and interpolate is disabled.
Create a new sphere, attach a rigidbody component and this script to it, it should work.
var speed:float = 2.0;
var resistance:float = 2.0;
var isMovingForward:boolean;
function Update() {
if (Input.GetKey ("w")) {
isMovingForward = true;
}
}
function FixedUpdate() {
if (isMovingForward) {
rigidbody.AddForce (0, 0, speed * resistance);
}
}
this did not work. I have found that AddForce only works when i dont use gravity…is it not possible to move my object if i use gravity on my rigidbody?
Try increasing the numbers, it may be adding force, but not enough to overcome gravity.
Try setting speed to higher numbers until the object moves.
I feel so silly. I dont know why I didnt even try to do that lol! Well thanks for pointing it out to me.