How to get the child collider of your own player when there is a collision between two players.
Hi,
I need a little help with detecting which collider my car hits another car with. Each car has several collider, but the one I'll be focusing on is the front collider. If I hit another car with my front end, I need it to add extra force to the hit. I don't care where I hit the other car, I just want to know, if I hit the car with my front collider (my child collider) add the extra force against the other car.
I've tried this:
private void OnCollisionEnter(Collision collision)
{
// If the object we hit is the enemy
if (collision.gameObject.tag == "Player")
{
if(collision.collider.name == "FrontCollider")
{
float theirMagnitude = collision.gameObject.GetComponent<Rigidbody>().velocity.magnitude;
float yourMagnitude = thisRigidbody.velocity.magnitude;
float difference = thisRigidbody.velocity.magnitude - collision.gameObject.GetComponent<Rigidbody>().velocity.magnitude;
Debug.Log("hit");
// Calculate Angle Between the collision point and the player
Vector3 dir = collision.contacts[0].point - transform.position;
// We then get the opposite (-Vector3) and normalize it
dir = dir.normalized;
// And finally we add force in the direction of dir and multiply it by force.
// This will push back the player
collision.gameObject.GetComponent<Rigidbody>().AddForce(dir * extraForce * thisRigidbody.velocity.magnitude);
}
}
}
The only problem is that when I collide with another car, it only works when I hit the front end of the other car. How do I ignore the other collider of the other car and only worry about my own child collider.
Thanks in advance. I appreciate it.
Comment