Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. 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. :)