Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by sharvey · Mar 31, 2011 at 09:20 PM · physicscollideroncollisionenter

Cancel a collision

Hi,

I'm trying to have different type of "Materials" collide with each others, with different effects depending on the types of the 2 colliding objects. For example, an object with material A should get destroyed when colliding with one made from material B, but not when colliding with material C.

The problem I have is that if I do the materials check in OnCollisionEnter, the collision has already happened, and even if I Destroy one of the colliding GameObjects, the other one still reacts to the collision.

What I'm looking for is a way to cancel a collision. When I destroy one of the colliding object, the other one should stay in it's play as if nothing happened.

Thanks.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Mar 31, 2011 at 09:23 PM

You could have a variable that keeps the velocity from the previous frame, and if the materials check doesn't pass, restore the velocity from the variable. Also probably another variable for angular velocity.

Comment
Add comment · Show 3 · Share
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 DaveA · Mar 31, 2011 at 09:38 PM 2
Share

Would be nice if there were a 'precollide' event one could overload. To the wishlist!

avatar image sharvey · Mar 31, 2011 at 09:44 PM 0
Share

Sound hacking, but it might just work. I'll try it tomorrow. Thanks.

I agree that there should be a callback for that.

avatar image sharvey · Apr 01, 2011 at 02:27 PM 0
Share

I worked, but it's quite nasty. Thanks!

avatar image
2

Answer by PlayCreatively · Jan 16, 2020 at 10:05 PM

I would like to add to Eric5h5's answer for those that look up this problem later on. keeping the previous velocity and angular velocity is the only way of doing this, I've tried plenty of other options. However, on impact not only does the velocity get affected by the impact but the object's position as well, immediately in the same frame. So you want to rectify that like so:

 Physics.IgnoreCollision(myCollider, collision.collider);
 rb.velocity = previousVel;
 rb.angularVelocity = previousAngVel;
 transform.position = previousPos + rb.velocity * Time.deltaTime;

Without the last line you will indeed keep your velocity but stop at the other collider for a visible frame, ruining the flow and looks clunky.

Comment
Add comment · Share
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

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

1 Person is following this question.

avatar image

Related Questions

How to get OnCollisionEnter working with Terrain. JavaScript 2 Answers

Character controller collider? 0 Answers

Need second pair of eyes: rigidbodies with colliders not firing OnCollisionEnter yet again... 1 Answer

driving game power ups 1 Answer

Creating a solid, immoveable object ? 1 Answer


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