Say you’re in your game, running, and jump up in the air, and start sliding along a wall to your right. How would you tell which side of you the wall is on? So far I have this collision detected as shown below but stuck on this part. Been toying around with move direction and such. Is there a way to do it with components of controllercollider hit or should I send out 4 rays in each direction and see which hits first?(Would this be bad practice? Single/MP game?)
Edit: I was able to resolve this by checking hit normal then comparing angle of the transform right(right/left) and forward(front/back) to see which of the 4 directions was facing the wall(45 degrees). Thank you for your help!
There are a couple of approaches to this problem. A character controller has a set of collision flags. The flags tell you what part(s) of the character were impacted. You can directly access these flags by CharacterController.collisionFlags. These flags are also returned by the CharacterController.Move() function. They don’t tell you which side, just that the sides were impacted.
Either independently or in combination with the collision flags, you can do some vector math using the hit point from the ControllerColliderHit parameter. To figure out which side, start by calculating the direction of the hit from the pivot point:
var dir = (hit.point - transform.position).normalized;
You can then get the angle between this vector and transform.forward, -transform.forward, transform.right and -transform.right. The side with the lowest angle is the one the hit is on. A slightly more efficient solution is to use Vector3.Dot() with these four directions.