- Home /
Question by
firepro20 · Oct 29, 2019 at 10:46 PM ·
spherecollidercontact.point
OnCollisionEnter with SphereCollider executes twice
ContactPoint[] contactPoints = collision.contacts;
for (int contactIndex = 0; contactIndex < contactPoints.Length; contactIndex++)
{
ContactPoint cp = contactPoints[contactIndex];
Collider otherObjectCollider = cp.otherCollider;
if (otherObjectCollider.gameObject.CompareTag("Collectible"))
{
Debug.LogError("Consumed Health!");
GameController.Instance.ReceiveHealth(gameObject, healthOnPickup);
//areaOfDetection.radius -= healthOnPickup; disabled for training purposes
if (collectibleList.Contains(otherObjectCollider.gameObject))
{
//Debug.Log("I have eaten " + collision.gameObject.name);
collectibleList.Remove(otherObjectCollider.gameObject);
//Debug.Log("And there are now " + collectibleList.Count + " in list.");
otherObjectCollider.gameObject.GetComponent<BoxCollider>().enabled = false;
otherObjectCollider.gameObject.SetActive(false);
//Destroy(collision.gameObject);
}
// remove from list then destroy, otherwise null pointer exception
//SetReward(1f);
//AddReward(0.045f);
AddReward(0.6f);
Done();
return;
}
}
The code above is executing twice, as most probably the circle collider is hitting two contact points when touching. How can I make this to be one touch/point only? ConsumeHealth! is being shown twice currently.
Comment
Answer by Lawlets · Oct 30, 2019 at 06:41 AM
You can use OnCollisionEnter instead of checking contacts points.
I tried to do that at first, without checking contact points however that and this code achieving same result
Your answer
Follow this Question
Related Questions
gravity problem 1 Answer
Sphere Collider with Terrain 0 Answers
c# bullet holes appearing on collider 1 Answer
Find Contact Point on touch 1 Answer