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

Continuous dynamic rigidbody entering collision a little

Discussion in 'Physics' started by Yacker, Feb 12, 2019.

  1. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    I have a sorta physics reliant game that mostly uses its own physics, but still relies on a rigidbody for collision and such, of course. I have it set to continuous dynamic, but it still has an awful tendency to clip into where it's moving, to an extent. This happens to spell catastrophe for me, as when moving along sloped surfaces, it will go into them a bit, and I spherecast down to keep the player attached to slopes that move downwards. When the player enters the geometry, they can't spherecast it anymore, and can sometimes even clip straight through the ground because of this, if there's collision below the sloped surface. Is there any way I could hope to get around this clipping issue?

    I hope my only solution here isn't a bizarre chain of spherecasts for movement instead of using the rigidbody normally.
     
  2. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    You could try decreasing Fixed Timestamp in Editor->ProjectSettings->Time so that physics updates happen more often. Be careful though since this can really chew up CPU cycles.
     
    Yacker likes this.
  3. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    That's not really an option. I already have the fixed timestamp at 0.016, and I don't want the requirements for my game to be incredibly high. And even still, that wouldn't really solve the problem, it'd just push it down the road to occur at higher speeds, and I want to account for basically any possibility.
     
  4. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    It's hard to say. I don't know how fast your player is moving, when he's clipping is it whole body, legs, is it even humanoid.
     
    Yacker likes this.
  5. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    It's just a default sphere collider with a rigidbody. It, realistically, should occur at basically any speed, but it occurs to the extent that it causes me such dire problems at a velocity of about ~50 in a direction.

    I could try creating a stripped down version of the project with a simple test scene demonstrating the issue, if that'd help.
     
  6. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    Did you try assigning a physics material to the "terrain" collider?
     
    Yacker likes this.
  7. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    No, the level collision has no physics material. If I were to apply one, what settings on it would I use?
     
  8. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I'm not an expert in this area, but would try a material that has zero bounciness setting. Pick a slope to test, and assign the material to its collider and test it out. See if it makes a difference.
     
    Yacker likes this.
  9. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    A physics material with bounciness set to 0 applied to the level geometry doesn't appear to produce any different results, unfortunately.
     
  10. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    Hmm. If you have a simple version of the project that you can share I can have a look.
     
    Yacker likes this.
  11. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    Here's a heavily stripped down version of the game, removing basically everything but the most basic physics. The test scene is set up with an example of the issue: when the player (the sphere) is set to a speed (which is a public variable on the attached script) roughly 40 or less, they go up the slope with no issue. If it's set to 50 or higher, they clip through it, because of a combination of the slight clipping into it by unity's physics, and my spherecast to keep it grounded on downwards slopes.
     

    Attached Files:

  12. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    The plain is horizontal in the project should there be an angle to the slope? What is the angle that you're using?

    Update: I set the plane to -41 degrees on the Z-Axis with speed of 60 on the sphere and I don't see any clipping. Do you mean the sphere penetrates the slope at some intervals, it that what I'm supposed to see. I see a smooth movement up the slope. I tried downward slope as well with 33 degrees down and the sphere slides nicely down the slope.
     
    Last edited: Feb 12, 2019
    Yacker likes this.
  13. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    There should also be an included model of a curved surface. If Unity's removed it from the scene for some reason, it's test.fbx in the root Assets folder, and should be scaled to 200,200,200, and placed roughly like so:
     
  14. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I'm using 2017 Unity, it warned me about importing and generally newer projects are not supported but it did load it in the end and seem to be working fine. But I don't see this model you're mentioning. There is Missing Prefab warning.

    Update: I added the model to the scene manually. Let me have a look.

    Update2: I had to add a mesh collider to the model using the mesh from the model for the collider mesh. With speed of 70 on the sphere it does not clip. It simply goes up the curved ramp and files into the air vertically. Even when I set the speed to 1000 it flies up and does not go through the ramp, it stays on it perfectly. Is the problem for you that it goes right through the ramp? Or partly into it?
     
    Last edited: Feb 12, 2019
    Yacker likes this.
  15. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    That's weird. I loaded it up in 2017 myself to see if it was a version problem, since I normally used 2018, but - after fixing the scene - I get the same results. It also seems like it doesn't occur when moving fast enough, with a speed of 700 working fine, but a speed of 70 not. You're running it at the correct framerate/fixed timestamp and all, correct? I'd imagine a lower framerate would have the same effects as speeding it up. Also, the scale matters. It occurs at 200,200,200 scale, but not 100,100,100 scale, which is the model's default. Specifically for 70 speed, anyway; it still actually occurs at 50 speed at the default scale.

    For clarification on the issue: Unity's physics makes it go partly in, which causes my code make it go straight through.
     
  16. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I see it at 70. Strange let me have a look.
     
    Yacker likes this.
  17. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    Can you try this. Recreate your curved ramp with higher triangle count and see if you still experience the issue.
     
    Yacker likes this.
  18. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    Bringing it up to a decently absurd number of triangles (6,144 - very high poly for a simple curve) displays no difference, I'm afraid.
     
  19. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I'm not sure what your script does but I think there might be an issue there. Here is why. I disabled the plane completely and just run the player with 70 speed, it then goes up the ramp without any issues. Just let it move horizontally as though there is a plane and let it go up the ramp. Also if I move the ramp at the end of the plane as in the picture it works fine. I think when the plane and ramp are close to each the player get's confused as to which it should follow. I don't know what you do in your script but I think the issue is there. When I tilted the plane and ramp and applied gravity and disabled your script the ball moved correctly so this leads me to believe that the script is doing something it should not. You mentioned you're sphere casting, put some debug statements and make sure what you're hitting is what you're supposed to be hitting. Also the sphere cast approach might not be optimal. I never coded a game that has these kind of mechanics (this is kind of like Mario Cart) but I would read up a bit on the net to see if there is a better approach to build this kind of track and keep the player on it. Sorry I couldn't be of more help.
     

    Attached Files:

    Yacker likes this.
  20. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    Yeah, it is the script that causes the particular clipping issue. I've been aware of that; spherecasts won't collide with anything the origin point of the cast intersects with. This is something I can't really avoid, which is why the problem lies with Unity allowing the player to clip into the floor a little bit. This causes the spherecast's origin to intersect the floor, ignoring it and hitting what's beneath.

    Obviously, this could be avoided simply by designing levels to not put other floors beneath slopes. However, this is also an issue, as it supports player generated content; most notably legacy content from something else it was based on, which was made in another engine that doesn't have this issue, so a lot of the levels there suffer from this problem.

    I appreciate your help, though. I can just hope someone out there knows a possible solution here. I'd rather not resort to performing all my movement with spherecasts instead of the rigidbody's velocity, as that can potentially cause other problems down the line...
     
  21. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    I see. How big is your sphere cast? Could you not move the originating point for your cast higher up on the player (mid level or top of his head, etc), not sure if that would help. Also have you looked at CheckSphere, would that be an option? Perhaps multiple CheckSphere with increasing radius.
    Another option could be doing a CheckSphere getting list of colliders and doing a ClosestPoint on both then checking their squared distances to see which one is closer, then moving the player a bit higher so it rides on the top collider. I don't have time to go through your script in detail so am not sure if any of these suggestions will help at all.
     
    Last edited: Feb 13, 2019
    Yacker likes this.
  22. Yacker

    Yacker

    Joined:
    Oct 24, 2018
    Posts:
    34
    I believe you meant OverlapSphere; CheckSphere doesn't return any colliders, just a bool. However, that's definitely something I didn't consider, so I set it up with ComputePenetration...and it actually did the trick! Thank you so much! My collision is finally working as expected.
     
  23. JesterGameCraft

    JesterGameCraft

    Joined:
    Feb 26, 2013
    Posts:
    452
    Np. I'm glad it worked for you. And ya, it was the OverlapSphere I was thinking about. Good luck with your game.
     
  24. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    The physics system doesnt like scaled objects(through the transform scale)

    Try scaling it to the need size in blender or whatever your using to see if it helps