Wednesday, December 23, 2015

Vibrating Collision, and falling through collision in Unity

Big title huh! And nope, I have no pictures this time...

Let me describe my problem, or two problems that are very much related. Then I'll tell ya how I solved them.

Player Collision:
In the 3D scenes I move the Rabbit (player) around by altering the Transforms position in the X and Y. I place Physics 2D edge colliders all over the scene to stop the player going through houses and off the edges of the scenes playable area. The way the collisions work is that the edges push back the box that the Rabbit has at the bottom of it's image. Thus preventing the Rabbit from crossing these edges.

1. When moving the player against one of these solid areas you can witness a vibrating of the Rabbit. This is due to you pushing (moving) into the collision wall and the wall pushing back.

2. When pushing along one of these collision edges you can some times fall through them. Occasionally there is a frame rate hiccup and you end up in the middle of the Fountain or House etc... pretty bad as ya canna get out.

My Solution:
I referred to this page in the Unity Docs
http://docs.unity3d.com/Manual/ExecutionOrder.html

That shows not just the execution order in Unity, but also all of the different stages you can plug into. This is important as I have done all my movement code inside the Update() function. I move the Rabbit multiplied by the Tine.deltaTime() and this means that depending on how much time has passed to how much the actual Rabbit moves this frame. This has the effect that you move into the collision by semi-random amounts. If the deltatime is a bit much then you can jump into the edge collision and possibly go far enough in that you get forced to the other side.

Unity has some extra Physics functionality, namely the FixedUpdate() function. So I placed my actual movement code into this function and because of the nature of how it works, I now get pushed back in a more controlled way. This removes the bouncing effect and also with the smaller movement increments it means you cannot jump past the collision. I do the actual button pressed test in the usual Update function, and then I only move in the FixedUpdate function, and all I did was to remove the multiply by delatTime.

Hope this you some ideas on solving this issue if you have it.

Laters
Da Voodoochief

No comments:

Post a Comment