- Home /
Is this code inefficient? It is causing Lag.
I am sure this code is bad, can you guys tell me what it could be?
I am seeing a massive drop in fps when I start it up but it goes back up to about 40-50 but still much lower than when its off, which is 80-90 even 100+.
I might be going crazy though you never know.
The things I am sure are not causing it in the code.
The cameras are set to enable and disable once in and so is the entire players object once I get in the vehicle with the turret. So its not an extra camera issue I made double sure only one is enabled when using the vehicle and turret.
Here is the code. It is a turret on a vehicle with a weapon.
using UnityEngine;
using System.Collections;
public class TurretController : MonoBehaviour {
//Class to draw range and stuff from.
// private TurretClass turretClass;
public Transform barrel;
public Transform firePoint;
public GameObject projectileMain;
public GameObject muzzleEffect;
public float shotForce = 0;
public double shotDelay = 0;
private double endDelay = 0;
private int shotCount = 0;
public float range = 1000;
public enum RotationAxis {MouseX = 1 , MouseY = 2}
public RotationAxis turretRotXY = RotationAxis.MouseX | RotationAxis.MouseY;
public RotationAxis barrelRotXY = RotationAxis.MouseX | RotationAxis.MouseY;
public float turretSensitivityX = 400f;
public float barrelSensitivityY = 400f;
public float turretMinimumX = -360;
public float turretMaximumX = 360;
private float turretRotationX = 0f;
public float barrelMinimumY = -360;
public float barrelMaximumY = 360;
private float barrelRotationY = 0f;
public Quaternion OrigRotation;
public void Awake(){
endDelay = Time.time;
}
public bool Recharged()
{
return (Time.time > endDelay);
}
// Use this for initialization
void Start () {
// shotForce = _turretClass.GunRange;
OrigRotation = transform.localRotation;
}
// Update is called once per frame
void Update () {
Turret();
Barrel();
Shoot();
}
void Turret(){
if(turretRotXY == RotationAxis.MouseX) {
turretRotationX += Input.GetAxis("Mouse X") * turretSensitivityX * Time.deltaTime;
turretRotationX = ClampAngle(turretRotationX, turretMinimumX, turretMaximumX);
Quaternion XQuaternion = Quaternion.AngleAxis(turretRotationX, Vector3.forward);
transform.localRotation = OrigRotation * XQuaternion;
}
}
void Barrel(){
if(barrelRotXY == RotationAxis.MouseY) {
barrelRotationY += Input.GetAxis("Mouse Y") * barrelSensitivityY * Time.deltaTime;
barrelRotationY = ClampAngle(barrelRotationY, barrelMinimumY , barrelMaximumY);
Quaternion YQuaternion = Quaternion.AngleAxis(barrelRotationY, Vector3.left);
barrel.localRotation = OrigRotation * YQuaternion;
}
}
public static float ClampAngle (float Angle, float Min, float Max) {
if (Angle <-360) {
Angle += 360;
}
if(Angle > 360) {
Angle -= 360;
}
return Mathf.Clamp (Angle, Min, Max);
}
void Shoot(){
if(Input.GetKeyDown(KeyCode.Mouse0) && Recharged()){
Instantiate(muzzleEffect, firePoint.position, firePoint.rotation);
Instantiate(projectileMain, firePoint.position, firePoint.rotation);
endDelay = Time.time + shotDelay;
shotCount++;
}
}
}
I have a good feeling it has got to do with the rotation of the turret not the barrel.
So yeah, you guys see something bad here?
That code is fine, nothing slow in that. It must be something else.
Also that ClampAngle function isn't actually going to work right with certain sets of angles, but it might be ok with the small deltas you are making and the choices of angle.
Delta what now? The turret is working perfect, but its causing lag...or maybe its the initializing of the entire car that slows it down. Because 2 cameras have to go on and then the wheels...$$anonymous$$aybe its the wheels... hmmm. Nah can't be because I have a tank too and it causes the same kinda lag when the turret is activated.
If I deactivate the turret during play, the frame rate noticeably jumps up... and it drops to 4, 6 even 12 fps when I first get in the car with turret on. after a bit of turning and looking around it goes back to 20 fps or so less than normal around 60-50 fps.
Answer by Sisso · Sep 10, 2013 at 08:18 PM
I didn't see any clear problem. The only real possible problem is if method Shoot has a bug that it instantiate both objects each frame.
I don't thinkg that this is script is the problem, but it could be activating the problem. For example: I am empty scene only with static objects, no physic must be computed. But if you put a single ball with rigidbody you will loss many fps because the physic must be computed, but you can add 100 balls without lose any fps.
Try to dig better where you are losing by commenting some code in the script, like changes in transformation.
Your answer
Follow this Question
Related Questions
One big script or lots of small ones? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Public, private and getting stuff efficiency question. 2 Answers
How do you efficiently target many instantiated prefabs for tweening? 0 Answers