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?

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.