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. Dismiss Notice

Should rigidbody2D.velocity affect gravity?

Discussion in '2D' started by outtoplay, Jun 15, 2014.

  1. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    I'd expect this tiny script to allow a sprite character(or any sprite with a collider2d/rigidbody2d), to drop onto a platform and then move along it in x at 'speed'. But instead, it floats down at a diagonal angle til it hits the platform, then moves along it.. Does this sound like what should happen?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveForward : MonoBehaviour
    5. {
    6.     public float speed =5f;
    7.  
    8.  
    9.     void FixedUpdate ()
    10.     {
    11.         rigidbody2D.velocity = new Vector2(speed,0);
    12.     }
    13. }
    14.  
     
  2. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    T

    Hello, first check the FixedAngle in your rigidbody2D component, and set Interpolate to be Interpolate, then check if it works, if it doesnt, then try using this code:

    Code (csharp):
    1. public float YourSpeed;
    2.  
    3. void FixedUpdate()
    4. {
    5. //To move left use -YourSpeed to move right use +YourSpeed.
    6.         transform.Translate(Vector2.right * YourSpeed* Time.deltaTime);
    7. }[code]
    8.  
    9. I hope thats what you wanted!
     
  3. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Thanks for the input, I appreciate your time. What I was getting at was, I'd expect a sprite with rigidbody and gravity scale of 1 to fall. It doesn't. Instead, it descends slowly as it moves in the x direction at 'speed'. This seems wrong to me, was wondering if this was an issue with Physics2D.
     
  4. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297

    Are you using an animations?
     
  5. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    Nope. It's simple to test. drag any sprite into the scene add a collider2d and rigidbody2d. Add a platform under it(with collider2d), and apply the movement script above. That is not how I'd expect physics to respond. I'm basically wondering if this is a bug.
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    You're zeroing out any vertical velocity every frame, so it's impossible for the object to accelerate downwards.
     
  7. outtoplay

    outtoplay

    Joined:
    Apr 29, 2009
    Posts:
    741
    That makes sense, thanks. This seems to work fine.

    Code (CSharp):
    1. rigidbody2D.velocity = new Vector2 (transform.localScale.x * speed, rigidbody2D.velocity.y);
     
    Last edited: Jun 16, 2014