Search Unity

[Beginner] I don't understand the Rigidbody2D.MovePosition :(

Discussion in 'Physics' started by JobiJoba, Jun 1, 2017.

  1. JobiJoba

    JobiJoba

    Joined:
    Jun 1, 2017
    Posts:
    13
    Hello !

    I'm trying to make an object move when it is created using RigidBody2D.MovePosition, so it can collide with other objects.
    I was able to make it move using transform.position, so I figured it would work in a similar way with RigidBody2D.
    But, when the object is created, it just stands still and don't move.
    Here's the code I have :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class projectile : MonoBehaviour {
    6.  
    7.     private Rigidbody2D rb2D;
    8.     public Vector2 velocity;
    9.  
    10.     void Start () {
    11.         rb2D = GetComponent<Rigidbody2D>();
    12.     }
    13.    
    14.     void fixedUpdate () {
    15.         movement();
    16.     }
    17.  
    18.     void movement()
    19.     {
    20.         rb2D.MovePosition(rb2D.position + velocity);
    21.     }
    22.  
    23.     private void OnCollisionEnter(Collision collision)
    24.     {
    25.         Destroy(gameObject);
    26.     }
    27. }
    28.  
    So, from what I understand, my Rigidbody2D component has a .position with an x and a y, and the .MovePosition holds the next x and y my Rigidbody2D will have, so the goal is that the .MovePosition x and y are different from the .position x and y. So I created a Vector2D variable, that I set to 2 in Unity.
    Now, if I understood correctly, the .Moveposition x and y should be equal to .position.x+2 and position.y+2 at this point.
    But, it doesn't work, the object just stays still for some reason.

    I probably understood something wrong, but I'm not sure what exactly.

    Thanks a lot in advance if anybody has an answer !
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I used forum.MovePosition to put this thread in the physics forum.

    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,507
    MovePosition basically does the same thing as transform.Translate but it respects the Physics system and keeps it abreast of the change to its physical values such as velocity and position, etc.

    If your object has a Rigidbody, use MovePosition to move it instead of direct Transform adjustments. Otherwise, the physics system will (well, could) not know that something has changed.
     
    zchek and JobiJoba like this.
  4. JobiJoba

    JobiJoba

    Joined:
    Jun 1, 2017
    Posts:
    13
    Thanks for the answers !

    So... The problem is that the MovePosition in my code doesn't use the Physics system as it should ?
    I tried adding the " * Time.fixedDeltaTime " indicated in the documentation also, but it doesn't change anything.
    I'm not sure what I'm missing here, I felt like I had everything needed to make it work.
    Am I doing it wrong ?
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,507
    Velocity is a (inappropriately named) public variable in your script, are you changing it at all? If not, then its just sitting at zero and nothing will move since that is what you're telling the rigidbody to move by.
     
    JobiJoba likes this.
  6. JobiJoba

    JobiJoba

    Joined:
    Jun 1, 2017
    Posts:
    13
    Yes, it has two variables, x and y, I both set them to " 2 ".
    The name "velocity" is a bit confusing indeed, I will change it for "speed" maybe, thanks !
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,507
    Ah, also.

    FixedUpdate()

    not

    fixedUpdate()
     
    roxioxx and JobiJoba like this.
  8. JobiJoba

    JobiJoba

    Joined:
    Jun 1, 2017
    Posts:
    13
    Ooooh, right ! Thanks a lot, it does work now !
    Well, that was stupid, I will try to be careful of that now.
    Thanks again !
     
    roxioxx likes this.