- Home /
Rigidbody.Addforce not working in Unity 5.4.1
Hello! In my game, I’m trying to get my player to jump, and in past games it’s worked, but for some reason now it just won’t do anything. I don’t get any errors, but whenever I press space, nothing happens. Why is this happening? Is it the version of Unity I’m using (5.4.1), or what? I tried looking at the script reference, but there's not even an example in Javascript. Here’s my code, if that helps:
#pragma strict
public var jumpSpeed = 10;
function FixedUpdate()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent.<Rigidbody>().AddForce(new Vector3(0, jumpSpeed * Time.deltaTime, 0));
}
}
Answer by CubeCrafter360 · Oct 17, 2016 at 07:24 AM
make a public rigid body //call it rb make a public vector 3 // call it force and were you want to use addforce. you do rb.addforce(force);
then in the inspector change the values of the vector 3 and it works
Answer by CubeCrafter360 · Oct 17, 2016 at 07:24 AM
public Rigidbody rb;
public Vector3 force;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(force);
}
}
}
Answer by CubeCrafter360 · Oct 17, 2016 at 07:24 AM
make a public rigidbody and called it rb then make a public vector 3 call it force then do rb.addforce(force) instedad and change the values of the vector 3 in the inspector and add the rigid body to the script