Search Unity

RigidBody2D.MovePosition is not moving to the exact position given to it?

Discussion in 'Scripting' started by farazk86, Sep 28, 2019.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    I'm trying to move a floating skull object towards the player using RigidBody2d.MovePosition using the below code:

    Code (CSharp):
    1. private GameObject shooter;
    2.     private Vector2 shooterPosition;
    3.     public float speed;
    4.     private Rigidbody2D rb2D;
    5.    
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         rb2D = gameObject.GetComponent<Rigidbody2D>();
    10.         FindShooter();
    11.     }
    12.  
    13.     void FindShooter()
    14.     {
    15.         shooter = GameObject.FindGameObjectWithTag("Player");
    16.         if (shooter != null)
    17.             shooterPosition = shooter.transform.position;
    18.     }
    19.  
    20.     private void FixedUpdate()
    21.     {
    22.         rb2D.MovePosition(rb2D.position + shooterPosition * Time.fixedDeltaTime * speed);
    23.     }
    The skull does move but for some reason it does not take the exact position of the player and moves to a few units ahead of the player.

    As can be seen below, I am constantly spawning the skulls with the above script and they all take the position ahead of the player.. Why is it not moving TO the player?



    Thanks
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The parameter for MovePosition is the target position. You want to be aiming for
    shooterPosition
    rather than
    rb2D.position + shooterPosition
    .

    Try something like
    rb2D.MovePosition((shooterPosition - rb2D.position).normalized * Time.fixedDeltaTime * speed);
    . This is untested, but the idea is to base the movement on the direction (difference between the two points).

    Incidentally, can your player move? If so, then you may find this code aiming the skulls only at the players start location.
     
  3. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Thanks but this is not working, they just teleport from the starting position to the cetre of the screen for some reason and then stay there.

    My version of the code was based on the documentation here: https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    The method takes an absolute position. The calculation I gave you calculates the offset. Therefore, instead of moving the object by a small amount it moves it to a position that is 'a small amount', i.e. close to zero which is effectively the centre of the screen.

    The fix is trivial. Have a go to see if you can fix it, but if not :
    Code (CSharp):
    1.     private void FixedUpdate() => rb2D.MovePosition
    2.         (rb2D.position +
    3.             ((shooterPosition - rb2D.position).normalized
    4.                 * speed
    5.                 * Time.fixedDeltaTime));
     
    farazk86 likes this.