Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by elzbeb · Jun 24, 2021 at 09:00 PM · rigidbodyoncollisionenter

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oscarAbraham · Jun 25, 2021 at 06:29 PM 0
Share

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.

avatar image elzbeb oscarAbraham · Jun 25, 2021 at 09:40 PM 0
Share

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

avatar image oscarAbraham elzbeb · Jun 26, 2021 at 12:41 AM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting an array of Rigidbodies 1 Answer

OnCollisionEnter cant work with wheel colliders 1 Answer

Problem with gravity, OnCollisionEnter and OnCollisionExit 2 Answers

OnCollisionEnter returning incorrect collision points 0 Answers

rigidbody.Addforce stacking? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges