Reputation: 51
Why is OnCollisionExit
not being called? I am using both OnCollisionEnter
and OnCollisionExit
but unfortunately only OnCollisionEnter
is being called.
public bool HandleCollided = false;
public void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "RightHandAnchor")
{
HandleCollided = true;
}
}
public void OnCollisionExit(Collision col)
{
if(col.gameObject.name == "RightHandAnchor")
{
HandleCollided = false;
}
}
Upvotes: 3
Views: 4039
Reputation: 11
Try to use OnCollisionEnter()
and OnTriggerExit()
!!
Example:
void OnCollisionEnter(Collision collision)
{
if((collision.gameObject.GetComponent<AttributeManager>().attributes & doorType) != 0)
{
this.GetComponent<BoxCollider>().isTrigger = true;
}
}
private void OnTriggerExit(Collider other)
{
this.GetComponent<BoxCollider>().isTrigger = false;
}
Upvotes: 0
Reputation: 51
Unfortunately using OnCollisionExit did not work so instead I used OnTriggerEnter and OnTriggerExit. I activated "isTrigger" for both objects.
public void OnTriggerEnter(Collider col)
{
Debug.Log("entered");
if (col.gameObject.name == "RightHandAnchor")
{
HandleCollided = true;
}
}
public void OnTriggerExit(Collider other)
{
Debug.Log("exit");
if (other.gameObject.name == "RightHandAnchor")
{
print("No longer in contact with " + other.transform.name);
HandleCollided = false;
}
}
Upvotes: 1
Reputation: 440
It's impossible to tell why your code isn't working based the given snippet - this code depends on the configuration of each of the GameObjects'
inspector windows in the editor.
Both the GameObject
that this script is attached to and the colliding GameObject
must have one Collider
component attached to each of them (for example, a BoxCollider
component or a SphereCollider
component). Both Colliders
must have their isTrigger
checkboxes disabled. The GameObject
that this script is attached to must also have a Rigidbody
component attached.
In order to debug this situation, add Debug.Log()
statements in your functions. This is generally good practice, and it might be that the function is being called but the conditional statement is not true.
Here are some additional ideas on what might be going wrong:
GameObject
somewhere else in your code.GameObject
.HandleCollided
was being changed elsewhere in your code.col
, is not what you expect.public void OnCollisionEnter(Collision col)
{
Debug.Log("Collision Enter!");
Debug.Log(col.gameObject.name);
}
public void OnCollisionExit(Collision col)
{
Debug.Log("Collision Exit!");
Debug.Log(col.gameObject.name);
}
Upvotes: 3
Reputation: 119
So you said "There are two objects that collides with each other. One has sphere collider attached to it and another has box collider. One of the objects have rigidbody attached as well." On which one is the code? and yes this matters! only 1 of the objects will keep track of the exit meaning that if it's the non-kinematic it will not work.
Upvotes: 1
Reputation: 1736
You need a non-kinematic rigidbody attached to your object to get the event for OnCollisionExit
Upvotes: 0