By observing the image below, I have a laser coming out of the red box with a 2D box collider moving to the left. On the other side I have a triangle with a 2D edge collider as you can see, and that collider is attached to a gameObject that have a tag equal to “Mirror”. And that small rectangular shape that is sticking out of the triangle is another laser that will extend upwards when the first laser collides with the mirror.
However that is not the case, when the first laser collides with the “Mirror”, the first laser stops as it should be, but the second laser starts moving but it very quickly stops moving as well, I want the second laser to keep moving after the first laser collides with the “Mirror”.
Here is the code I have currently that is somehow not working, I have tried everything I could.
This is the code that will run when the laser collides with a “Mirror”:
public class WhenLaserCollides : MonoBehaviour
{
public Animation laserAnimation;
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.tag == "Mirror")
{
laserAnimation.Stop();
}
}
}
This is the code that will run when a “Mirror” gets hit by a laser:
public class WhenLaserHitMirror : MonoBehaviour
{
public GameObject laser;
public Animation laserAnimation;
void OnTriggerEnter2D (Collider2D col)
{
laser.SetActive (true);
laserAnimation.Play();
}
}
Not sure what your actual use case is here, but perhaps after a laser collides with a given collider, you could turn it off to avoid future collisions?
Then when the laser beam is all done travelling, perhaps go through and turn the colliders back on? Not sure if that would work the way you need, such as having multiple lasers, etc.
Another way would be to have each laser keep track of the colliders that it has seen and don’t collide more than once with them in a given “run,” but then the laser cannot go in a circle, or else the next time through it will fail to collide with the “known” colliders.
Another way is to write some code to decide if the laser is “approaching” or “leaving” and only trigger the collision behavior on approach, which would allow it to leave unmolested on its way out.
Wow, I can’t believe I overlooked disabling the collider, that worked perfectly, thanks mate. I use this as a reflection system, it will look like that the laser is reflecting.
And when the laser finishes travelling and you want to flip all the colliders back on, you can just get all colliders in your scene and re-enable them at once before the next laser shot.