Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Newbie question on moving a rigidbody2D around

Discussion in '2D' started by stoilcho, Sep 18, 2014.

  1. stoilcho

    stoilcho

    Joined:
    Sep 8, 2014
    Posts:
    30
    Hello guys,
    I'm new to Unity. I'm working on a top-down 2D game where you control your character RTS style by clicking the mouse around.

    Right now, I only have single sprite setup with a rigidbody2d with a bit of liner drag and unaffected by gravity. Trouble is my movement works right only of first click. After the first click as I keep clicking around the rigidbody starts to deviate off course for no apparent reason. Obviously my code is somehow causing Vectors to add up in a way that changes direction. But I'm still learning this stuff - could somebody help me out and point out what it is that I'm doing wrong. I've been at this for bit now and I'm really frustrated. Thank you! Here's my script:

    Code (CSharp):
    1. public class PlayerBehavior : MonoBehaviour
    2. {
    3.  
    4.         // Use this for initialization
    5.         void Start ()
    6.         {
    7.                 targetDestination = rigidbody2D.position;
    8.                 rigidbody2D.angularVelocity = 0;
    9.                 walk = false;
    10.         }
    11.  
    12.         public float speed;
    13.         private Vector2 targetDestination;
    14.         bool walk = false;
    15.  
    16.         void Update ()
    17.         {
    18.                 //We should always get nput in Update();
    19.                 if (Input.GetMouseButtonDown (0)) {    
    20.                         targetDestination = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    21.                         rigidbody2D.velocity = new Vector2 (0, 0);
    22.                         walk = true;
    23.                 }
    24.         }
    25.  
    26.         // Update is called once per frame
    27.         void FixedUpdate ()
    28.         {
    29.                 if (walk) {
    30.                         var momentaryPush = targetDestination;
    31.                         momentaryPush.Normalize ();
    32.                         rigidbody2D.AddForce (momentaryPush * speed); //Use this for multiple force pushes with equal force
    33.                 }
    34.  
    35.                 if (Vector2.Distance (targetDestination, rigidbody2D.position) < 0.1f) {                    
    36.                         rigidbody2D.position = targetDestination;
    37.                         rigidbody2D.velocity = new Vector2 (0, 0);        
    38.                         walk = false;
    39.                 }
    40.  
    41.         }
    42. }
     
  2. stoilcho

    stoilcho

    Joined:
    Sep 8, 2014
    Posts:
    30
  3. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Your code is only valid if the Player is at 0,0; anywhere else, and he's not going to head towards the target (instead he's moving based on the vector from 0,0 to the target).

    Easy fix on line 30:

    var momentaryPush = targetDestination - transform.position;