Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple control of an object using rigidbody2D

Discussion in 'Getting Started' started by KillCon, Mar 18, 2015.

  1. KillCon

    KillCon

    Joined:
    Mar 18, 2015
    Posts:
    2
    Hi

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

    Code (CSharp):
    1.     public float speed = 10.0f;
    2.  
    3.     void FixedUpdate ()
    4.     {
    5.         float moveHorizontal = Input.GetAxis("Horizontal");
    6.         float moveVertical = Input.GetAxis("Vertical");
    7.        
    8.         Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
    9.  
    10.         rigidbody2D.AddForce(movement * speed * Time.deltaTime);
    11.  
    12.     }
    It is like rigidbody2D only exists as Rigidbody2D and the AddForce properties do not exist.

    Please help!
     
  2. Effervescent

    Effervescent

    Joined:
    Mar 7, 2015
    Posts:
    31
    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:

    Code (csharp):
    1. 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).
     
    Ryiah likes this.
  3. KillCon

    KillCon

    Joined:
    Mar 18, 2015
    Posts:
    2
    Thanks, it works. :)