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

rigidbody2D.MovePosition() doesn't apply gravity?

Discussion in 'Scripting' started by blackbeetle, Oct 15, 2014.

  1. blackbeetle

    blackbeetle

    Joined:
    Jun 12, 2014
    Posts:
    4
    Why rigidbody2D.MovePosition() doesn't apply gravity,but rigidbody.MovePosition() apply gravity.
    rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(keyboardX, keyboardY));
    rigidbody.MovePosition(rigidbody.position + new Vector3(keyboardX, keyboardY, 0));
    These codes run difference result!!
     
  2. LLTools_

    LLTools_

    Joined:
    Jun 27, 2015
    Posts:
    7
    I have this same problem. and dont solve this.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    did a little testing on this, since there was another thread which was talking about something similar...

    3d script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestScript : MonoBehaviour
    6. {
    7.     Rigidbody rb;
    8.     Vector3 movement;
    9.    
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.         Debug.Log("3D " + Physics.gravity);
    14.     }
    15.    
    16.     void Update()
    17.     {
    18.         float h = Input.GetAxis("Horizontal");
    19.         float v = Input.GetAxis("Vertical");
    20.         movement = new Vector3(h, 0, v);
    21.     }
    22.  
    23.     void FixedUpdate()
    24.     {
    25.         rb.MovePosition(rb.position + movement);
    26.     }
    27.  
    28. }
    29.  

    2d script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestScript2 : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     Vector2 movement;
    9.  
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody2D>();
    13.         Debug.Log("2D " + Physics2D.gravity);
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         float h = Input.GetAxis("Horizontal");
    19.         float v = Input.GetAxis("Vertical");
    20.         movement = new Vector2(h, v);
    21.     }
    22.  
    23.     void FixedUpdate()
    24.     {
    25.         //if(movement.sqrMagnitude != 0f)
    26.         //{
    27.             rb.MovePosition(rb.position + movement);
    28.         //}
    29.     }
    30.  
    31. }
    32.  

    result (green rigidbody2d / testscript2, blue rigidbody/testscript)


    if you include the commented out section of the 2d script so the moveposition is skipped if there is 0 input the gravity is applied correctly, however, doing a moveposition(currentposition + (0, 0)) results in gravity barely being applied.

    The 3d rigidbody works fine with the moveposition(currentposition + (0, 0, 0)) included throughout.
     
    Nicholas-Rishel likes this.
  4. Nicholas-Rishel

    Nicholas-Rishel

    Joined:
    May 7, 2013
    Posts:
    16
    @LeftyRighty Have you opened a bug report with the project from the gif?
     
    Last edited: Feb 19, 2016
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Its not a bug. It says quite plainly in the docs that rb2d.MovePosition does not apply gravity
     
  6. Nicholas-Rishel

    Nicholas-Rishel

    Joined:
    May 7, 2013
    Posts:
    16
    You're right about it not being a bug, but it is an inconsistency of behaviour of similarly named methods. I don't think the forum is the appropriate place to bring API differences to the attention of Unity for tracking, so the bugtracker seems to be the only place to document this.
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Rigidbodies, be it 2D or 3D, should be manipulated with velocities and forces, not translation. The reason for the gravity being "barely applied" is because the velocity is constantly being reset, so the velocity is resetted while gravity is re-applied on every frame.