Search Unity

How to prevent an object from falling through another with own gravity / vector collisions

Discussion in '2D' started by Deleted User, Aug 21, 2019.

  1. Deleted User

    Deleted User

    Guest

    I'm creating a jupiter lander clone using vectors to give me an understanding of the game maths but I've hit a small snag.

    I have a gravity constant and a collision layer. If my ship is travelling too fast when it hits an object it triggers Die(), if it is going less than that it will trigger Bounce()

    Bounce is working fine except that if I set my coefficientOfRestitution (how much of the impact force is reflected) to less than 1 the spaceship will eventually settle right on the object with the collider and then fall through until it hits my screen bounds.

    Is there a known formula for preventing this in Unity? I'm toying with either using raycasting to never actually collide with the collider or locking the bounce to a minimum value which prevents encountering this issue but I feel like there should be a more elegant solution.

    The full project is visible at on my github , apologies for the state of the code it's my first unity project I intend to refactor out all of the logic from the terrible huge playercontroller into better areas once the minimum functionality is in place.