- Home /
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);
}
}
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)
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);
}
}
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.
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.
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?
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
}
}
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.