- Home /
CapsuleCollider : How to get the info which part is hit?
I have build a leaning function for my FPS character. to prevent him to look through walls I added 2 CapsuleCollider with rigidbodys around the camera. the outer Capsule stops the leaning so the camera cannot go farther on. the inner Capsule should move the camera a bit back to the center if I walk into a wall.
so now my problem is if I push the "lean right" button and it detects a wall it will stop move. thats good. but when I let the button go and push at the same time the "lean left" button it will still stop moving the camera because I only have this one Collider... same for the inner collider. I cannot determine if I hit a wall on the right side or on the left side...
as I wrote these lines I found a solution that could work but I still want to know if there is a simple line of code that I could add to my Colliders to determine which hemisphere is hit.
Answer by Adam-Buckner · Feb 05, 2013 at 04:27 PM
If you are new to scripting and new to the Unity engine, you may wish to look at Raycasting.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
And instead of using colliders, simply check with a ray (2, if you want to check left and right) for objects to each side.
With the information from the Raycast Hit:
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
... you will be able to find more details about what and where you've hit an object.
You can then apply your logic based on knowing where that object is.
the problem with a raycasti is that its just a thin line... and my character is leaning to the side and a little down. if it would just check the souroundings like that it would may glitch out on stuff that is standing around or when I am walking while leaning then I would end up with the camera in doors or something. and I would need then 4 raycasts 2 on each side. 1 to check if is there a wall and 1 to check if I am in a wall to move the camera back and then it would not work. I will maybe only use raycast for footstep sound. so the only way would be colliders with rigidbodys.
You could also look at sphere and capsule cast...
http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.CapsuleCast.html
Here is an explanation on how this could be used: http://www.theantranch.com/Unity/Entries/2011/3/29_Understanding_Spherecasts_in_Unity.html
This way you could cast a volume rather than a ray.
O$$anonymous$$G I did not know that this is possible! I have already solved the problem with coding http://youtu.be/kPx2evc5RO8?t=4m43s but I think I should replace my uncrouch collider with this because if I really try I can still stand up in walls. how much does this SphereCast affect the FPS? or is it like a Raycast?
Answer by Adam-Buckner · Feb 04, 2013 at 11:22 PM
You may want to look at:
http://docs.unity3d.com/Documentation/ScriptReference/Collision-contacts.html
If you look at the collision class, you should be able to get the contact points. I am assuming that if you compare the contact point to the center point of the object, you will be able to determine which direction it is in.
I should say I am very new to coding.... so I am cunfused... btw I found out that one side of the capsule collider reacts faster to collisions than the other side... when I am leaning to the right it stops the camera to late and the wall hit the inner capsule so it wanders back a little. when I lean to the left it stops also very late but not too late... why is that so?