- Home /
How to stop collision at specific distance?
Hi! I am making a mobile serious game in which a ball destroys a brick when hitting, the ball represents the player hand movement. A counter increases every time collision occur. How to stop collision when distance between the ball and the brick is less than a specific number, i need to force the player to move his hand in specific movements to achieve collision.
Answer by game4444 · Nov 20, 2018 at 04:57 AM
You could calculate the distance between the player and your brick. for Example: float distance = vector3.Distance(player.postion,brick.position); if(distance<2) { // stop player } this is one solution. 2nd solution you could do is you can attach collider with Trigger checked and increase the size of collider where you want to stop your player. And check whenever Player Triggers collider you could stop your player.
Sorry, but that would take the distance from the center, so you should also subtract half of the sphere.
Thanks for reply, I already calculated the distance, i don't want to stop player movement. all i need is to stop collision when the distance is for example less than 4, to force the player to move his hand because it is a forearm rehabilitation serious game.
Could you attach screenshot of your game setup for clear picture of problem. thanks
what about my second solution using triggered colliders around your brick wall?
Answer by Feref2 · Nov 20, 2018 at 05:06 AM
It´s difficult to tell because I don´t know a lot of variables, but I can tell you this. With Vector3.Distance you can get the distance between the center of the ball and a point on the brick (depends on what you need). Then just subtract the sphere radius from it and you will get the distance from the edge of the ball, of course, assuming that sphere is completely spherical.
Thanks for reply, I already calculated the distance, which sphere do you mean? the sphere collider?
What do you mean with "which sphere do you mean?" You only have one sphere, the one of the ball. I think the sphere collider has the same size, but if you are not sure how to do it then just use meshFilter.bounds.size.x / 2. You can also use the renderer or the z axis, whatever you need. The meshFilter is really a box I think, but its width is the same. Just draw a circle inside of a square, then a horizontal line from center to edge and you´ll get what I mean. I actually did it, but couldn´t upload the image. By the way, sorry for replying 3 days later. I didn´t saw this until now.