Search Unity

Question Collision with sharp edges

Discussion in 'Physics' started by partimevillain, Nov 29, 2022.

  1. partimevillain

    partimevillain

    Joined:
    Nov 8, 2022
    Posts:
    5
    Hello everyone,
    I'm very new to unity and have ran into a problem I couldn't find answers for.
    upload_2022-11-28_21-7-0.png

    In the image above, you could see the terrain, fence and trees, have rough and sharp edges.
    Often times when it collides when the character, the player will do an abrupt "jump" to unexpected locations.
    I am 90% sure this is due to the rough outline of the terrain. I thought about adding collider boxes
    around the terrain of make things even but if I have to do that for every piece of terrain, it could take up a lot of time. Are there any faster and more reliable methods to fix this problem?
    I am using my tiny-RPG-Forest in the asset store. be perfect if I could just fill up the square/tile.
    thank you
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Presumably you're correctly moving by manipulating the Rigidbody2D correct? Adding forces, modifying velocity etc? You're not teleporting the object to new positions via the Transforms right i.e. non-physical motion?

    The physics engine doesn't know what a "sharp edge" is so there's nothing to fix apart from finding out what causes the "jump".
     
    partimevillain likes this.
  3. partimevillain

    partimevillain

    Joined:
    Nov 8, 2022
    Posts:
    5
    upload_2022-11-29_12-32-51.png
    This is what I'm using for movement.
    I've tried both methods and they all give the same problem.
    I think the cause is the jagged edges that make contact with the character, say when the corner of the character's
    box collides with a caved in part of the terrain.
    It works fine when I manually put another collider box around the terrain so the character would not touch the sharp edges, but like I mentioned before, if I were to do this it will take up quite a long time.
    The sprites I'm using was imported from the asset store. so I didn't do the edges.
    Is it possible to fill up a square with invisible paint when I am creating the tilemaps?
    that way I get rid of the rough edges and can still have sprites with round or sharp surfaces.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    How can it do anything if you're stomping over the velocity constantly? How is it supposed to change the velocity to solve collisions?

    The problem is nothing to do with sharp edges. You are focused on that and are trying to think about sharp edges and how to avoid them without even knowing what is causing the issue. Focus on conveying what the problem is here and not the solution.

    So to be clear, you've not shown the actual issue yet. I wrote the physics here at Unity and I don't know what the problem is you're describing. Maybe show a video or something.

    Also, you need to describe the elements that are interacting clearly. I have this "bush" and it's a tilemap and I use X and Y. I move a RB with a X collider using ABC. Here's the video of the problem which only happens during Q. That kind of stuff. Don't wait for others to ask you everything then you'll get answers.
     
    partimevillain likes this.
  5. partimevillain

    partimevillain

    Joined:
    Nov 8, 2022
    Posts:
    5
    ok I've uploaded a video that shows the problem.

    I have trouble recreating it consistently but you could see it happening in the video twice.
    The first time the character runs into the left fence (about 11 seconds in) and a little after ( 26~27 seconds ish).
    you could see he abruptly jumps to a different location rather than staying at the fence.

    Both times I was only holding down the left ( A ) movement key.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    The video is private.
     
    partimevillain likes this.
  7. partimevillain

    partimevillain

    Joined:
    Nov 8, 2022
    Posts:
    5


    gosh I am so sorry. seems like I am doing every wrong today.
    it should be available now
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    It's not easy to see what's going on here. Your movement is 10m/s and I presume your camera is attached directly to the player because the player is not moving in the view.

    When the box overlaps the edges (it will because you're using Discrete collision detection on the Rigidbody2D) then it modifies the velocity to move it out of the overlap but you have to understand that you're stomping over this so it can only do so much to solve the overlap. Set the Rigidbody2D to Continuous is more expensive but should stop overlaps so you don't get the problem caused by you stomping over velocity.

    Also, if you're going to mount the camera to the player which is using 2D physics, you should understand that by default, physics is running during the FixedUpdate which isn't per-frame so your motion will be terrible. This is why there's an Interpolation option on the Rigidbody2D so that it updates the Transform per-frame (not the Rigidbody2D position).

    Either that or you are free to run the physics per-frame (see Simulation Mode in the Physics Settings).
     
    partimevillain likes this.
  9. partimevillain

    partimevillain

    Joined:
    Nov 8, 2022
    Posts:
    5
    I see. That has solved the issue.
    Thank you very much. I've learned quite a lot through this.
     
    MelvMay likes this.