Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Strict Gravity Control

Discussion in 'Physics' started by Nikovich, Jan 7, 2015.

  1. Nikovich

    Nikovich

    Joined:
    Jul 10, 2010
    Posts:
    62
    I'm designing a 2D platforming game and while the physics system makes platforming much easier to deal with, I would like greater control on the gravity. I want the character's jumps to be floaty, like Luigi's. I also want the character to fall relatively fast if they walk of a ledge. I've gone about this by changing their gravity scale to a lower number when they jump, and bringing it back to a higher number when they hit the ground.

    However, like just about all 2d/planar games I can imagine, I also want to make sure that his fall speed never exceeds the maximum speed of his jumps. Something I seem to have trouble finding is a way to limit gravity to a maximum value; either for the character or for the entire game. I know you can set limits on an object's total magnitude, but I don't want to cap their speed along the X axis. I just want to make sure that his fall speed stops once it hits a number defined by me. I haven't found a way to do this through inspector values, such as the rigidbody properties or the physics settings. Am I missing something or would I be looking to do this with code?
     
  2. TiG

    TiG

    Joined:
    Feb 28, 2011
    Posts:
    308
    if(rigidbody.velocity.y>maxSpeed) rigidbody.velocity.y=maxSpeed

    or something like this in FixedUpdate will limit the velocity in one direction
     
  3. A_never_kill

    A_never_kill

    Joined:
    Jan 7, 2014
    Posts:
    79
    Hi.

    You can do one thing.
    Just go the properties of the physics2d in the project setting.;)

    Thanks
     
  4. Nikovich

    Nikovich

    Joined:
    Jul 10, 2010
    Posts:
    62
    Hm... I was kind of hoping there would be a better way than that. I'm a little hesitant about directly adjusting velocity, especially continuously, but if that's the best way of doing it, I suppose it wouldn't be too bad for a simple side-scroller.


    And then what? Like I said in the first post, I didn't see anything in there about limiting maximum gravity speed. I see a max translation speed, but I also said I don't want to limit the X movement, just the limit of incrementing gravity.


    Edit: Tried out TiG's method; seems to work fine, though I read there can be oddities with directly affecting velocity. I guess the "issues" most people talk about is that it would be unrealistic physics. If you're making a classic style platformer however, more direct control on the physics isn't really a bad thing. The only thing I would add, if someone is new, you can't directly assign a value to a specific axis, you have to make a new vector 2 (or 3), so it would look more like

    Vector2 newSpeed = new Vector2(rigidbody2D.velocity.x, maxSpeed);
    rigidBody2D.velocity = newSpeed;

    Anyway, thanks, though I'm still interested to know if there is something more direct in the physics settings.
     
    Last edited: Jan 9, 2015
  5. TiG

    TiG

    Joined:
    Feb 28, 2011
    Posts:
    308
    Directly adjusting velocity can lead to strange behaviour when you don't know what you are doing.However I do use the method from time to time with good results. A more natural way to limit velocity would be applying a terminal velocity. This may be done with adjusting Unity's own drag coefficient or writing your own to specify a terminal velocity direclty. But this is a much more advanced method, and may not be suitable for everyone.