- Home /
How to destroy a projectile when it leaves any side of the camera view on a 2D game?
Hello, I have tried searching the Questions for a resolution on this, but could only find one where the projectile would be destroyed upon exiting the camera on the right side of the screen while the firepoint is in the middle of the camera. I have been trying to get the projectile to be able to be destroyed, but I can't figure out the proper equation and have it destroying after it leaves the camera in about 2 seconds. I just don't want it being able to destroy any baddies that haven't been shown yet but are still in the stage. Below is what I'm working with code wise, and I'm wondering about trying to even devise a way to create a variable for the distance measured between the minimum x camera // maximum x camera verses the firepoint position, but I don't know if that is the only way to solve this, and even then I'm not too good with logical thinking like this.
This is the Update method in the script that controls the trajectory of the projectile:
void Update()
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D>().velocity.y);
Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,0f));
Vector2 firePosition = (new Vector2 (player.GetComponent<Rigidbody2D> ().position.x, player.GetComponent<Rigidbody2D> ().position.y));
if(transform.position.x > position.x || transform.position.x < -position.x - firePosition.x)
{
Destroy(gameObject);
}
}
Answer by Seb-1814 · Aug 30, 2015 at 03:37 PM
A simple solution would be to use OnBecameInvisible message.
void OnBecameInvisible() {
Destroy (gameObject);
}
Hope this helps
Hmmm, this seems to be set to the #Scene window in Unity and not the actually camera bounds I have following the character. Thanks though.
@xylofiso Have you tried to build the project and test this again? That should actually work, one thing to note is that the objects count as visible as long as they can be seen by any camera, which includes the scene view when you're in the editor.
Ins$$anonymous$$d of building, you could also just turn away the scene camera so that the camera rendering your game is the only one that sees your projectile.
@Seb1814 There's nothing wrong with your suggestion. It was just an explanation due to the comment that the function does not fullfill the needs, but it actually does. Just not while using the Unity editor (unless you turn the scene camera away from the object). It should work as expected as soon as the game is built.
Of course there are more elegant ways, like object pooling. But that could be improved later on.
It worked when I played the game maximized, didn't try this the first time. Thanks a bunch, quick and simple solution!
Your answer
Follow this Question
Related Questions
2D Android Game Projectiles becoming invisible 0 Answers
How to move camera with arrow keys while also using Cinemachine Follow + Look Ahead 0 Answers
Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers
Destroy clone when left Field of view? 1 Answer
Scene goes pink when I go left. 0 Answers