Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Gravity Disturbing Ascents and Descents on The Map

Discussion in 'Physics' started by FGPArthurVII, Jul 16, 2016.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    As gravity forces you down, It's easy to go down "ramps", more than It is to Walk actually, but to climb up "ramps" It's pretty hard, you try to go up, gravity pulls you down, and don't even think about releasing the walk button because gravity pulls you all the way back down, maybe after one minute you can finish the climbing. And I can't decrease gravity otherwise things start to feel like moon. And all these differences between going up, down or straight is pretty bothering. How can I match it right?
     

    Attached Files:

  2. Oribow

    Oribow

    Joined:
    Nov 9, 2012
    Posts:
    38
    What you are describing is just normal physic behaivior. Rigidbodys are meant to work with physics. As I see it, you now have two options: 1. leave the rigidbody alone and use a custom moving solution (good starting base https://github.com/prime31/CharacterController2D) 2. come up with a formular, that adjust the movement force according to the gravity on a slope (difficult here is a pic:)
     
  3. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    Yeah I know, but the rigidbody in the case is the main character, I wanted some way to ignore this fact on the ramps, like he automatically was adding force on the legs to climb without the player have to do it and noticing
     
  4. Oribow

    Oribow

    Joined:
    Nov 9, 2012
    Posts:
    38
    Okay. Then the difficult way. (Haven't test the following) First you take the normal of the surface the character is standing on. Then you rotate it by 90 degrees, so it becomes parallel to the surface the character is standing on. Then you take the dot product of that and the gravity vector. (You're calculating how much of the gravity vector lies in the direction of the parallel vector)

    Code (CSharp):
    1. Vector2 normal;
    2. Vector2 gravity = Vector2.down * 9.81f; (earth gravity)
    3. Vector2 normal;
    4. Vector2 pullingForce = new Vector2(normal.y, -normal.x); //rotate the normal by 90 degrees
    5. pullingForce = Vector2.dot(gravity, pullingForce);
    6.  
    In theory, you now only have to apply the opposite of the pullingForce and thus canceling it out.

    Example: You stand on a entirely flat surface
    Code (CSharp):
    1. normal = (0, 1);
    2. gravity = (0, -9.81);
    3. pullingForce = (1, 0);
    4. pullingForce = dot(gravity, pullingForce) = (0, 0) // because no amount of the gravity vector lies in the direction of the pF vector
     
  5. Oribow

    Oribow

    Joined:
    Nov 9, 2012
    Posts:
    38
    Forgot to mention, that you should cap the slopes angle, because if the slope is completly vertical, this calculation cancel the whole gravity force out. Just only remove slope influence, if the slopes angle is below a ceratin amount of degrees.