- Home /
OnCollisionStay2D is ignoring OnCollisionEnter2D
I have a round player with Circle Collider 2D and obstacles with Edge Collider 2D. When player touches one obstacle, player changes color and keeps changing colors and expanding until player moves away from obstacle, but if the player touches the second obstacle while still touching the first, the game is restarted. I have a problem with detecting second obstacle. Sometimes second obstacle is detected and my OnCollisionEnter2D works fine, but most of the time it doesn't. I feel like program is doing only OnCollisionStay2D and it is ignoring OnCollisionEnter2D so in the meanwhile player touches one more obstacle while still touching the first but the game is not restarted. It feels like it is doing OnCollisionStay2D forever. Collision detectiod: Continous
void OnCollisionEnter2D (Collision2D other){
if (other.gameObject.tag == "Obstacles" && wasDetected == false)
{
wasDetected = true;
Debug.Log (other.gameObject.name);
}
else if (other.gameObject.tag == "Obstacles" && wasDetected == true)
{
wasDetected = false;
Debug.Log (other.gameObject.name);
theGameManager.RestartGame();
}
}
void OnCollisionExit2D (Collision2D other){
wasDetected = false;
}
void OnCollisionStay2D (Collision2D other){
if (other.gameObject.tag == "Obstacles")
{
transform.localScale = new Vector3 (transform.localScale.x + 0.1f, transform.localScale.y + 0.1f, transform.localScale.z);
myColor.color = new Color(myColor.color.r + 0.05f, 0, 0);
}
}
Answer by Bale_txy · Jan 31, 2018 at 02:30 AM
try moving all codes inside OnCollisionEnter to OnCollisionStay, that should help
If I move all the code form OnCollisionEnter to OnCollisionStay then player will die at the very beginning of the game. I forgot to mention that most of the time player collides with the obstacle but OnCollisionEnter2D is not called.
Answer by melsy · Feb 01, 2018 at 06:18 AM
is there a rigidbody2d on the player?
You need to put a tag if on the exit as well. Otherwise any exit at all will trigger that. The easiest way to find the problem is to put a Debug.Log(stay,enter,exit) in the callbacks. That way you can see where it is malfunctioning
There are just obstacles is my game, but i also tried to put if statement and it didn't solve the problem.
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D works without being actually triggered. 0 Answers
Detecting empty collider 1 Answer
OnCollisionEnter 1 Answer