Need help with collision Detection
Hey so i wanted to know what can i do to detect if i am touching a wall other than raycasts and tags, i already tried this in OnCollisionEnter
if (Math.Abs(Vector3.Dot(other.GetContact(0).normal, Vector3.up)) < 0.1f)
the problem that its detecting multiple times when i move my camera. [Here][1] is an example, and [here][2] is another one.
If anyone knows someway to get consistent results when doing a 360 on a wall while detecting one time that would be great. [1]: https://gyazo.com/60d48fa50a2495bfdb4bbe6e20556ad9 [2]: https://gyazo.com/4f0989cd6264a340b4ffde4425a02b80
OnCollisionEnter
shouldn't fire constantly if it's not constantly exiting and entering. Does OnCollisionExit
fire multiple times too? If that's the case, I'll say it's not technically wrong to set an isTouchingWall
flag on Enter and unset it on Exit.
That is, if there aren't multiple colliders in your RigidBody, because a collider could be constantly touching while another keeps entering an exiting. In that case, you could ignore the colliders in your RB that don't matter by using Collision.GetContacts and ContactPoint.ThisCollider to check which collider was hit. Or you could keep a count that augments OnEnter and decreases OnExit, and only unset the wall when all your colliders have exited.
Although, maybe you should use OnCollisionStay
instead. That's because your character could start touching the side of a cube collider, which gets detected as a wall, then get on top of it. When it's on top of it, the cube is no longer a wall, but you'll never receive the OnCollisionExit callback because the collision was never exited.
Why don't you want to use raycasts, though? See, if collision noise ends up being too much, and there's a good reason to keep it like that, you might want to detect walls that are very close, even when they are not touching. Usually, one would use triggers for that, but they don't work here because they don't give out normals. What'd I'd do is use PhysicsCapsuleCast with a capsule that's a bit bigger than my collider, and use the resulting hitInfo to know if the character is touching a wall.
Hey, so i am using OnCollisionEnter because when i am wallrunning and i jump off a wall, if i detect a wall i add a little boost up to keep my momentum going and not just be affected by gravity and fall. Here is an example, i am talking about the boost that is applied just after i hit another wall https://gyazo.com/54c6ba99792ee468a59c1f3b7965c1a1
Also i discovered that when i move my camera it detects a new collision here is an example of that (which i dont want) https://gyazo.com/b74f4aa63f2f7cd244a9d0d11e9000bb
I got that when you move your camera it detects a new collision, but I'm pretty sure that the only reason that's happening is because a collider is also moving. So, you could start by checking what's changing the rigid body's position; maybe it's not perfectly centered, maybe there rigid body's shape is not symmetrical. Maybe there's a lot of friction and it makes the character's RigidBody do little jumps. Maybe the floor is very slippery... There could be a lot of reasons, but I can't know which one without more access to the code/project.
If you want a quick and dirty to solve it, maybe you can decouple the camera's rotation from your rigid body. I believe Cinemachine already has functionality for that. If you need the camera to rotate with the mesh, then you'd also need to decouple the mesh rotation from the rigid body.
Another posible quick and dirty solution, if I get things right from your gif, is that you could only apply the boost if the player's velocity's product perpendicular to the wall is above certain threshold. It would eli$$anonymous$$ate the extra callbacks from rotating the camera, as the character's velocity towards the wall should be almost zero then. A nice thing about it is that you could apply more o less boost depending on how hard the character hits the wall.
I'm I correct in presu$$anonymous$$g that you're using OnCollisionEnter
because you only want to detect first moment a wall is touched? You could still do that with a Capsule cast in FixedUpdate: When you detect collision with a new wall, you store a reference to the wall's collider in a list and do the boost if you're jumping. When you stop detecting a particular wall's collider touching the casted capsule, you remove it from the list so you can boost from it again.
Also, I forgot to say, in case it helps: If you're using a Character Controller, you should use OnControllerColliderHit and related callbacks instead of OnCollisionEnter.
Your answer
Follow this Question
Related Questions
Collider seems bigger than it is 0 Answers
Why is my Rigidbody freaking out like this? 0 Answers
OnCollisionEnter with two kinematic Rigidbodies? 1 Answer
Getting an array of Rigidbodies 1 Answer