Simple control of an object using rigidbody2D

Hi

I have watch some tutorials about controlling characters, but it is not working for me.

    public float speed = 10.0f;

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
       
        Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

        rigidbody2D.AddForce(movement * speed * Time.deltaTime);

    }

It is like rigidbody2D only exists as Rigidbody2D and the AddForce properties do not exist.

Please help!

I think the problem is with Line 10 and, depending on whether you are using Unity 5 or not, there is also a problem with the rigidbody2D bit. In Unity 5 it’s:

gameObject.GetComponent<Rigidbody2D> ().AddForce (movement * speed * Time.deltaTime);

I think the previous version it’s just Rigidbody2D (without the GetComponent bit but I can’t remember).

1 Like

Thanks, it works. :slight_smile: