Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by el-mas-pro-470 · Jul 09, 2019 at 04:54 PM · collisionerrorcolliderboxcollider

OnCollisionEnter wheel collider error!

Hello! I have a problem with "on collision enter". The script is about when my car collides, life is subtracted, but the problem is that it also detects the WheelCollider, and each time it rolls, it hurts itself because it detects the collision of the wheels and that of the Box Collider of the car.

How do I ONLY detect the BOX COLLIDER of the car?

Thank you!

 using UnityEngine;
 using System.Collections;
 
 public class DestroyedCar : MonoBehaviour {
 
     public float crashforce;
     public float carHealth;
     public float carMediumHealth;
     public GameObject text;
     public GameObject smoke;
     public BoxCollider colider;
 
     void OnCollisionEnter(Collision col){
         
         if(col.relativeVelocity.magnitude > 10){ //remove this 
             
             carHealth -= crashforce * col.relativeVelocity.magnitude ; //line 1
             
         }  //remove this
         
         if ( carHealth <= 0 ){
             
             DestroyCar();
             
         }
         if ( carHealth <= carMediumHealth ){
             
             smoke.SetActive(true);
             
         }
         
     }    
     
     void DestroyCar () {
 
         text.active = true;
         transform.GetComponent<Car>().enabled = false;
 
         transform.GetComponent<Car>().WheelTI.brakeTorque = 100f;
         transform.GetComponent<Car>().WheelTD.brakeTorque = 100f;
         transform.GetComponent<Car>().CenterOfMass = new Vector3(0,0,0);
 
     }
 }
 
 
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Magso · Jul 09, 2019 at 07:23 PM

Put all the code in *OnCollisionEnter() under this if(other.GetType() == BoxCollider){ or if (col is BoxCollider)

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
avatar image
0

Answer by I_Am_Err00r · Jul 09, 2019 at 07:47 PM

This should work:

 using UnityEngine;
 using System.Collections;
  
  public class DestroyedCar : MonoBehaviour {
  
      public float crashforce;
      public float carHealth;
      public float carMediumHealth;
      public GameObject text;
      public GameObject smoke;
      public BoxCollider colider;
     GameObject player;
 
     void Start()
     {
         player = gameObject;
     }
  
      void OnCollisionEnter(Collision col)
     {
     if(col.gameObject == player)
     {
          
          if(col.relativeVelocity.magnitude > 10){ //remove this 
              
              carHealth -= crashforce * col.relativeVelocity.magnitude ; //line 1
              
          }  //remove this
          
          if ( carHealth <= 0 ){
              
              DestroyCar();
              
          }
          if ( carHealth <= carMediumHealth ){
              
              smoke.SetActive(true);
              
          }
     }
          
      }    
      
      void DestroyCar () {
  
          text.active = true;
          transform.GetComponent<Car>().enabled = false;
  
          transform.GetComponent<Car>().WheelTI.brakeTorque = 100f;
          transform.GetComponent<Car>().WheelTD.brakeTorque = 100f;
          transform.GetComponent<Car>().CenterOfMass = new Vector3(0,0,0);
  
      }
  }
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 Magso · Jul 09, 2019 at 08:18 PM 0
Share

This is checking if it's colliding with itself. Also all children colliders will call OnCollisionEnter on the parent which is the main problem @el-mas-pro-470 is having.

avatar image I_Am_Err00r Magso · Jul 09, 2019 at 08:42 PM 0
Share

You are absolutely right @$$anonymous$$agso, I put that answer up without really understanding the problem, thanks.

First, I see you have the BoxCollider as public, try adjusting it to private and declare it in Start:

 void Start()
 {
 collider = GetComponent<BoxCollider>();
 }

I might be wrong, but that should only detect the things that enter the parent collider and ignore all the children ones.

What you will want to do is add a tag to whatever object will cause it damage, usually it's "Enemy".

then change this line:

 if(col.gameObject == player)

to:

 if(col.gameObject.tag == "Enemy")


That should work, I don't have an IDE that I can test with right now.

avatar image el-mas-pro-470 I_Am_Err00r · Jul 10, 2019 at 01:52 AM 0
Share

Thanks for the help I tried and neither of the two options turned out ...

It happens to me also, that when there is a division of colliders (a long street with two divided meshes for example) the car goes through that division, it is hurt, by the wheelcolliders. Is there any way to assign the car box collider only?

avatar image
0

Answer by Servail · Jul 12, 2019 at 10:59 AM

If you just need to prevent some Colliders from firing Collision calls on your script simply put them on a different GameObject(s).

If you need to determine if certain Collider is hitting, use:

 foreach (ContactPoint contact in collision.GetContacts())
 {
     if (contact.thisCollider == boxCollider)
     {
         //Do stuff
         return; //if you just need a collision fact
     }
 }
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
avatar image
0

Answer by uniqe · Jul 12, 2019 at 12:08 PM

kindly show the inspector and the hierarchy in which the collider and the script is present to make the problem clear.

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

207 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

Related Questions

Error CS1061 I can't find a solution! [C#] 1 Answer

Big issue with colliders 1 Answer

Stop sword from distorting character animation 3 Answers

2D Colliders aren't actually touching but they're behaving like they do? 0 Answers

Multiplayer Layers and Colliders confusion. 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