简体   繁体   中英

Why does this cause my Unity game's frame rate to plummet?

I am making a basic Unity 3D FPS game. Tonight I tried to add the functionality to shoot at things so that when I hit them they dissolve into fire and are destroyed. I accomplished this using the raycast function, and when I went to add the fire and destruction(with the shown code), and it worked just as I wanted: The targets dissolved into fire (muhaha). I went on to shoot at a few more targets, and on the fourth one my frame rate suddenly plummeted from 71 frames per second to 10 . I did some more tests with the same results: No matter how long I waited before shooting, the FPS still plummeted on the 3rd or 4th one. I shot at target in different orders. Same result.

The targets do not have any code on them, and are basically just boxes. The fire that I am using is the fire that comes as an asset with Unity (is it a problem with lights?).

I thank you in advance for helping, and feel free to comment with additional questions.

Variable declarations:

public LineRenderer bulletTracer;
public float fireSpeed = 4.0f;
float fireTimer = 0.0f;
bool bulletShot = false;
Ray ray;
RaycastHit hitObject;
public GameObject gun;
public GameObject scope;
public GameObject fire;

Code:

ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));

if (fireTimer > fireSpeed && 1 == Input.GetAxis("Fire1") && Physics.Raycast(ray, out hitObject)){

    fireTimer = 0;

    //Makes a really nice line from the gun to the position that I hit.
    bulletTracer.SetPosition(0, gun.transform.position);
    bulletTracer.SetPosition(1, hitObject.point);

    bulletShot = true;

    //if you hit a target or a pickup....
    if(hitObject.collider.attachedRigidbody.gameObject.tag == "target" & hitObject.collider.attachedRigidbody.gameObject.tag == "Pickup")
    {
        Instantiate(fire, hitObject.transform.position, new Quaternion(0, 0, 0, 1)); //spawns fire at the hit object
        Object.Destroy(hitObject.collider.attachedRigidbody.gameObject); //destroys that object
    }       
}

EDIT: I found my problem involving print statements. Thank-you to all that helped me.

You are not using an object pool. Instantiate and destroy are expensive. Instead of instantiating these things now, have them already loaded and just enable/ disable and reposition appropriately.

If its still laggy it could be because of the fire and the amount of object with that same effect.

Also, use Qusternion.identity. dont create a new one.

Good luck!

There was a commponent that is was not assigned correctly, and at kept on returning errors to the console, and each time I shoot an object the errors come in faster. I quick deletion of a component fixed the lag.

If you don't have a profiler, you can see the Statistics ("Stats" button) panel. It also tells you how many milliseconds spent computing ("CPU") and rendering graphics ("render thread"). If the "render thread" time is too high, you might need to reduce your special effects (less fire particles?). If CPU took a long time, optimize your code: For example, use object pools as @TheNoob said. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM