How Can I Know which collider has call OnCollisionExit() on the script's gameObject??
In Code I Have Writed. im trying to know my plane have landed. then i need to know all my wheels is Ground. but i need to do it like this :
[SerializeField] // for see whats going on
private List<bool> inContact = new List<bool>();
[Header("Options :")]
public List<Collider> Wheels;
// Will Set it From inspector.
void Start()
{
foreach (Collider col in Wheels) inContact.Add(false);
}
private void OnCollisionEnter(Collision collision)
{
if (Wheels.Contains(collision.contacts[0].thisCollider))
{
inContact[Wheels.IndexOf(collision.contacts[0].thisCollider)] = true;
// Knowing That Wheel is inTouch.
}
}
private void OnCollisionExit(Collision collision)
{
if (Wheels.Contains(collision.contacts[0].thisCollider))
{
inContact[Wheels.IndexOf(collision.contacts[0].thisCollider)] = false;
// Trying To Know That Wheel Take-Off
}
}
This is not working because collision.contacts.Length is 0 at OnCollisionExit. Then This way cannot help me, But i need To know which MY collider called OnCollisionExit function because this plane have too much Collider and i just need wheels...
(After Edit)
I Did What i Want.
This Code For Landing Areas:
public class LandAreaScript : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player" && collision.gameObject.GetComponent<PointSystem>() != null)
{
collision.gameObject.SendMessage("GetLanded", collision.collider);
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Player" && collision.gameObject.GetComponent<PointSystem>() != null)
{
collision.gameObject.SendMessage("GetTakeOff", collision.collider);
}
}
}
And This for my plane:
public class PointSystem : MonoBehaviour
{
[SerializeField] // for see what is going on
private List<bool> inContact = new List<bool>();
[Header("Options :")]
public List<Collider> Wheels;
// Will Set it From inspector.
void Start()
{
if (Wheels.Count == 0) this.enabled = false;
foreach (Collider col in Wheels) inContact.Add(false);
}
public void GetLanded(Collider Input)
{
if (Wheels.Contains(Input))
inContact[Wheels.IndexOf(Input)] = true;
}
public void GetTakeOff(Collider Input)
{
if (Wheels.Contains(Input))
inContact[Wheels.IndexOf(Input)] = false;
}
}
It is working now. But at other projects
Am i need to Set this LandAreaScript to all objects???
No way to do That. Will not Optimize.
I Get Dissappointed by Unity.
WHY THERE IS NO WAY TO GET COLLIDER which one at same object with script?
We need a Read-only variable at Collision Class like "thiscollider".
Did you ever find a solution to this? I also need thisCollider on OnCollisionExit()
Answer by tormentoarmagedoom · Feb 27, 2020 at 02:53 PM
Hello.
MAybe you are focusing in the worng direction. A solution can be to have a collider+sript in each wheel. In this script you detect the collisions for that specyfic wheel, and have some public bool variable to determinate if the wheel is grounded or not.
From your "general" script then, check for all this bools in each wheel script, so no need to know the number of collisions finished or started, you just check for the bool variable state.
This works for you?
Your answer
Follow this Question
Related Questions
Player cannot destroy enemy 1 Answer
Colliders for tunnels 0 Answers
How i can fix a player controller bug? 0 Answers
Team Pong Colliders not working 0 Answers
Collision Problems 1 Answer