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);
}
}