- Home /
How many ways to stop player from going through walls?
So I'm trying to find a way to stop my character from going through walls, but here is the catch, without using rigidbodies.
So far I can use rayCasting in 8 directions (as the collider is a capsule I need at least 8 rays to go in the main directions) but I find this limited as future non-square colliders on the objects may cause problems with my actual code (which is far from perfect) that finds the point of impact of the ray and stops the character from going over that coordinate.
I tried to use spherecast at direction Vector3.zero and distance 0 (to make it stay around my character) but it doesn't work.
Thought of using CheckSphere or OverlapSphere but it doesn't return the point of impact (as far as I know).
Is there another option? I don't like the in-built physics calculations as in the past I found ways to cheat them and so I don't trust them very much. Example: used to put invisible walls around my map to keep the character in the interested area, but under certain conditions I was able to pas them. The I started using Mathf.Clamp on transform.position and never had a problem since then.
Any suggestion is welcomed.
Answer by Kishotta · Sep 23, 2017 at 06:21 PM
Physics.ComputePenetration is meant for exactly this sort of problem!
Thanks, I can definitely use this method, much easier than calculating the coordinates of the overlapping point and then manually limit the position of the character.
So I tried this and it works perfectly. Just changed a little bit of the code so I don't have to specify the number of colliders that I interact with. Thanks again.