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 not detecting gravity

Discussion in '2D' started by SFDCIVimyProject, Nov 2, 2015.

  1. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    I created a Character (by creating an Empty Game Object) and added the following Components to it:
    • Transform(already attached)
    • Rigidbody2D
    • Sprite Renderer
    • Polygon Collider 2D
    • Animator (after creating Animations)

    When I run the game I expect the Character to 'fall' because of gravity and the Rigidbody2D that is attached, but nothing happens.
    Only the Default Animation runs through its frames.

    After doing a bunch of testing (i.e. adding other Game Objects to the game that do 'fall') I realize the lack of gravity and Rigidbody2D reaction must have something to do with the Animations that I've added to my Character. I've even tried adding a script that 'AddsForce' to the Rigidbody2D but it does not move at all.

    When I created the animations, on the default animation I added a 'Curve' that Transforms the Position slightly and I figure this must have something to do with overriding the Rigidbody2D but I cannot for the life of me figure out why or fix it.

    Please help
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Adding a Rigidbody2D obviously puts it in control of the Transform position. By setting the Transform position directly, you're undermining that because when you set the Transform directly, it'll set the Rigidbody2D position, stomping on anything that the physics system does to move the Rigidbody2D.

    In other words, you're asking it to do two conflicting things.
     
  3. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    I understand that but I'm not using any sort of script to change the Transform position at all.

    My understanding is that if you apply a 'Curve' to the Animation it is relative to where the sprite is located... not a global adjustment to the Transform's position. Am I thinking about this incorrectly?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,563
    Agreed, it's not a script, it's the Animation component as you've already said that is changing the Transform. Whether the change is relative or not still doesn't alter the fact that you're updating the Transform position and likewise, this causes the Rigidbody2D to sync with it i.e. you're controlling the position of the Rigidbody2D via the Transform.

    When animating in such a scenario, the Rigidbody2D should really be set to kinematic as you're controlling it directly. Kinematic bodies are also not affected by gravity.
     
  5. SFDCIVimyProject

    SFDCIVimyProject

    Joined:
    Oct 21, 2015
    Posts:
    9
    Gotcha... thank you very much!