- Home /
Multiple Colliders malfunctioning
Hi guys, I'm sorry if this turns out to be long one. Basically, I have 2 objects colliding, both have several trigger colliders on them to detect the depth of collision. The biggest collider is attached to the parent, and the rest are its children, empty game objects with a collider and a rigidbody. Here's an illustration:
Each of the colliders are on different physics layers and can collide with themselves only. They have a simple script attached that checks collision and sets a variable to true. Here's an example:
public void OnTriggerEnter2D(Collider2D other)
{
isCollidedFirst = true;
Debug.Log("collider1");
}
public void OnTriggerExit2D()
{
isCollidedFirst = false;
}
And in update there's an if loop that checks for the key press and based on the boolean variables from the collision scripts executes some actions such as stopping the objects, destroying them, etc.:
if (Input.GetKeyDown(KeyCode.Space))
{
if (Object3.isCollidedThird == true && ObjectFinal.isCollidedFinal == false)
{
objectLeft.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
objectRight.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
Destroy(objectLeft, 0.5f);
Destroy(objectRight, 0.5f);
canRotatePlayer = false;
Object3.isCollidedThird = false;
canSpawn = true;
Debug.Log("Key pressed - Third collision registered");
}
else if (Object2.isCollidedSecond == true)
{
objectLeft.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
objectRight.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
Destroy(objectLeft, 0.5f);
Destroy(objectRight, 0.5f);
canRotatePlayer = false;
Object2.isCollidedSecond = false;
canSpawn = true;
Debug.Log("Key pressed - Second collision registered");
}
else if (Object1.isCollidedFirst == true)
{
objectLeft.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
objectRight.GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
Destroy(objectLeft, 0.5f);
Destroy(objectRight, 0.5f);
canRotatePlayer = false;
Object1.isCollidedFirst = false;
canSpawn = true;
Debug.Log("Key pressed - First collision registered");
}
else
{
Debug.Log("nothing");
}
}
The thing is, this doesn't work always. It malfunctions often. Sometimes the collision wouldn't register immediately or at all. Sometimes a key press would be registered before the next collision happens: I know that the physics things, such as the movement stop, should be in FixedUpdate(), I have tried that, it still behaves the same way. It seems to me that the OnTriggerEnter2D function doesn't have enough time to send the information to Update, although that sounds stupid. The objects are not moving really fast, so I doubt that's the reason.
Thanks for any help.
Your answer
Follow this Question
Related Questions
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
Trigger Spawning? 1 Answer
Detecting another object collision from another object script (2D) 0 Answers
Can't click gameobject when over another trigger? 1 Answer
Stopping my player from moving when hitting a wall. 5 Answers