I’ve looked at everything I can about rigidbodies but for some reason mine won’t move. HEres the code (Under fixed update).
if (isMoveR)//if its moving right
{
rigidbody.AddForce(new Vector3 (speed*Time.deltaTime,0,0));
Debug.Log("right");
isMoveR = false;
}
the bool isMoveR is true if the ‘d’ key is pressed.
My guess is that you are not adding enough force. The default mass of your rigidbody is 1, and it takes a bit of force to get it moving. If this is a one-time event (i.e. you are using something like Input.GetKeyDown() that only returns true for a single frame), then you likely want something like:
rigidbody.AddForce(new Vector3 (1000.0f,0,0));
If you are adding each frame, then use Time.deltaTime, but you will still need a significant amount of force. Note that you are not applying ‘speed.’
Is your variable “speed” an integer? Are you specifying its type? If you don’t, and if you initialize it at declaration to be something like “var speed = 1;” then it will likely be considered an integer.
Then when you multiply it by a Time.deltaTime and that delta is less than 0.5, it may get truncated down to a zero. The result would be that your object would not move.
Perhaps specify the variable as a float or when you initialize it, use “var speed = 1.0f”. Here’s an example of how static/dynamic typing combined with integer roundoff can cause things to fail.
// countdown is inferred to an integer value
var countdown = 5;
function Update () {
// countdown will never decrease!!!
countdown -= Time.deltaTime; // T.dt gets rounded to 0
}
By definition ‘AddForce’ should be applied every frame to see its effect.
I think what you are looking for is to apply impulse to the body instead of just plain force. You should do ‘AddForce’ with ForceMode.Impulse.