- Home /
Destroy clone when left Field of view?
I did this before but now I can't remember how I did. I have a space shooter and for performance reasons I want to destroy bullets that travel out of the Camera's visibility. Here's the current code for my gun shooting if that helps.
var bullet : Rigidbody;
var Spawn : Transform;
var shotspeed : int = 1000;
var offset = Vector3(0,0,3);
var firingRate : float = 0.1;
var lastFired : float = -100;
function Start ()
{
gameObject.SetActiveRecursively(true);
}
function Update ()
{
if (Time.time < lastFired + firingRate) {
return;
}
if(Input.GetButton("Fire"))
{
Toss();
}
}
function Toss()
{
shot = Instantiate(bullet,Spawn.position+offset,Spawn.rotation);
shot.rigidbody.AddForce(transform.forward * shotspeed);
lastFired = Time.time;
}
Answer by WilliamLeu · Mar 06, 2013 at 02:01 AM
Check if renderer.isVisible is false.
I've tried this but what do I link the destroy function to? I can't get it to destroy the clones. If i try it either doesn't work or throws the error that entities can not be deleted?
Yea, note that it only returns true if it's not visible in EVERY camera, and the editor camera counts.
In terms of recognizing when it is no longer visible, you have a couple of choices. You can convert the position to Viewport coordinates. If x or y is less than 0 or greater than 1, then the camera is not seeing that specific point. Another way is to use these two functions:
GeometryUtility.CalculateFrustumPlanes
GeometryUtility.TestPlanesAABB
The second link as some example code of making the check.
Only one camera yes, the problem I'm having is pointing the destroy function to the clones of the original? The name of the original is "Pulseshot" so in the inspector I get Pulseshot(Clone) a bunch of times but I can't seem to get it to delete the clones?
What would I put in Destroy("******");
to make it destroy the clones of the object?
Your answer
![](https://koobas.hobune.stream/wayback/20220613093903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy gameObject leaves behind collider? 1 Answer
How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer
Placing a gameObject into, and pulling out of inventory 2 Answers
Camera frustums + custom field of view 2 Answers
Invisible Objects in Scene View 3 Answers