Search Unity

Why does my rigidbody(2D) accelerate?

Discussion in '2D' started by proggerwolf, Feb 1, 2015.

  1. proggerwolf

    proggerwolf

    Joined:
    Feb 1, 2015
    Posts:
    4
    I'm working on a 2d game where my player is intended to move along the x-axis with a constant velocity/speed. Despite setting the velocity of my rigidbody to a value, the rigidbody does accelerate.
    (note: the ground has got a friction of zero)

    Code (CSharp):
    1. void Start () {
    2.  
    3.         Physics2D.gravity = new Vector2(0, -9.81f*5);
    4.         rigidbody2D.velocity = new Vector2 (0.05f, rigidbody2D.velocity.y);
    5.         Debug.Log (rigidbody2D.velocity.x);
    6.      
    7.     }
    Has anybody got an idea why it does that ?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Obviously I can't see all your code but If you're not applying any forces (only setting velocity directly as in your post) then the only possible force that can be continuously causing such acceleration is gravity but this won't occur unless you're on a slope.
     
  3. proggerwolf

    proggerwolf

    Joined:
    Feb 1, 2015
    Posts:
    4
    Thanks for your reply. The issue was that my character was a child of my camera, therefore when my character moved and the camera followed him (The camera has its own script), the offset of the camera was also applied to the character.
     
    theANMATOR2b likes this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Glad you got it sorted.