My projects frame count is so low help

Well my project won’t rise above 2fps no matter what pc. Even high end ones. The builds lag slightly less. It only takes about 20 percent cpu usage on my pc and on my friends it was 3 percent. Draw calls is at 300 and verts is at 42k idk why this is happening the game seem to be well optimized from stats. It’s only on one project i’ll post all the code i warn you i may be a few thousand lines in it’s js and C# together varying by scripts.

Mouse Script

var defaultCameraAngle : float = 60;
@HideInInspector
var currentTargetCameraAngle : float = 60;
@HideInInspector
var racioZoom : float = 1;
@HideInInspector
var racioZoomV : float;

var racioZoomSpeed : float = 0.2;

var lookSensitivity : float = 5;
@HideInInspector
var yRotation : float;
@HideInInspector
var xRotation : float;
@HideInInspector
var currentYRotation : float;
@HideInInspector
var currentXRotation : float;
@HideInInspector
var yRotationV : float;
@HideInInspector
var xRotationV : float;
var lookSmoothDamp : float = 0.1;
@HideInInspector
var currentAimRacio : float = 1;

var headbobSpeed : float = 1;
@HideInInspector
var headbobStepCounter : float;
var headbobAmountX : float = 1;
var headbobAmountY : float = 1;
@HideInInspector
var parentLastPos : Vector3;
var eyeHeightRacio : float = 0.9;

function Awake ()
{


	parentLastPos = transform.parent.position;
	
}

function Update () 
{
	if (transform.parent.GetComponent(PlayerMovementScript).grounded)
		headbobStepCounter += Vector3.Distance(parentLastPos, transform.parent.position) * headbobSpeed;
	transform.localPosition.x = Mathf.Sin(headbobStepCounter) * headbobAmountX * currentAimRacio;
	transform.localPosition.y = (Mathf.Cos(headbobStepCounter * 2) * headbobAmountY * currentAimRacio) + (transform.parent.localScale.y * eyeHeightRacio) - (transform.parent.localScale.y / 2);
	
	parentLastPos = transform.parent.position;

	if (currentAimRacio == 1)
		racioZoom = Mathf.SmoothDamp(racioZoom, 1, racioZoomV, racioZoomSpeed);
	else
		racioZoom = Mathf.SmoothDamp(racioZoom, 0, racioZoomV, racioZoomSpeed);
		
	camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, racioZoom);

	yRotation += Input.GetAxis("Mouse X") * lookSensitivity * currentAimRacio;
	xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity * currentAimRacio;
	
	xRotation = Mathf.Clamp(xRotation, -90, 90);
	
	currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
	currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, yRotationV, lookSmoothDamp);
	
	transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
}

Player movement

var currentGun : GameObject;
var distToPickUpGun : float = 6;
var throwGunUpForce : float = 100;
var throwGunForwardForce : float = 300;
var waitFrameForSwitchGuns : int = -1;

var walkAcceleration : float = 5;
var walkAccelAirRacio : float = 0.1;
var walkDeacceleration : float = 5;
@HideInInspector
var walkDeaccelerationVolx : float;
@HideInInspector
var walkDeaccelerationVolz : float;

var cameraObject : GameObject;
var maxWalkSpeed : float = 20;
@HideInInspector
var horizontalMovement : Vector2;

var jumpVelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;

var crouchRacio : float = 0.3;
var transitionToCrouchSec : float = 0.2;
var crouchingVelocity : float;
var currentCrouchRacio : float = 1;
var originalLocalScaleY : float;
var crouchLocalScaleY : float;
var collisionDetectionSphere : GameObject;

@HideInInspector
var waitToPickupAmmo : float = 0;

var currentCell : GameObject;

var health : float = 100;
var maxhealth : float = 100;
var deadBodyPrefab : GameObject;
var texture1 : Texture2D;
var mat : Material;
var x : float = 0;
var y : float = 0;
var w : float;
var h : float;
function Awake ()
{


	currentCrouchRacio = 1;
	originalLocalScaleY = transform.localScale.y;
	crouchLocalScaleY = transform.localScale.y * crouchRacio;
	//GUI.Label(Rect(Screen.width/2.01,Screen.height/2.12,300,200),"X");
}
function Update(){
var healthy : float = 1-(health/1000);
if(healthy == 0){
healthy = 0.1;
}
mat.SetFloat("_Cutoff",healthy);

}

function LateUpdate () 
{
	if (grounded)
		rigidbody.useGravity = false;
	else
		rigidbody.useGravity = true;
	
	waitFrameForSwitchGuns -= 1;
	waitToPickupAmmo -= Time.deltaTime;
	
	transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentCrouchRacio);
	//if (Input.GetButton("Crouch"))
		//currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 0, crouchingVelocity, transitionToCrouchSec);
	//if (Input.GetButton("Crouch") == false && collisionDetectionSphere.GetComponent(CollsionDetectionSphereScript).collisionDetected == false)
		//currentCrouchRacio = Mathf.SmoothDamp(currentCrouchRacio, 1, crouchingVelocity, transitionToCrouchSec);
	
	horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
	if (horizontalMovement.magnitude > maxWalkSpeed)
	{
		horizontalMovement = horizontalMovement.normalized;
		horizontalMovement *= maxWalkSpeed;		
	}
	
	rigidbody.velocity.x = horizontalMovement.x;
	rigidbody.velocity.z = horizontalMovement.y;
	
	if (grounded){
		rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
		rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);}
	
	transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
	
	/*if (grounded)
		rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
	else
		rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
			*/
	if (Input.GetButtonDown("Jump") && grounded)
		rigidbody.AddForce(0,jumpVelocity,0);
	
	if (health <= 0)
	{
		currentGun.GetComponent(GunScript).beingHeld = false;
		currentGun.rigidbody.AddRelativeForce(Vector3(0, throwGunUpForce, throwGunForwardForce));
		Instantiate(deadBodyPrefab, transform.position, transform.rotation);
		collider.enabled = false;
		cameraObject.GetComponent(AudioListener).enabled = false;
		enabled = false;
		//Application.LoadLevel(5);
		Destroy(gameObject);
		
	}
	
	if(health < maxhealth)
	health ++ ;
	
	
}

function FixedUpdate ()
{
	if (grounded)
		rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration, 0, Input.GetAxis("Vertical") * walkAcceleration);
	else
		rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio);
}

function OnCollisionStay (collision : Collision)
{
	for (var contact : ContactPoint in collision.contacts)
	{
		if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
			grounded = true;
	}
}

function OnCollisionExit ()
{
	grounded = false;
}

function OnTriggerExit ()
{
	currentCell = null;
}

function OnTriggerStay (hitTrigger : Collider)
{
	if (hitTrigger.tag == "AIpathCell")
		currentCell = hitTrigger.gameObject;

	if (hitTrigger.transform.tag == "StairGoingUp")
		if (!Input.GetButton("Jump") && Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
			if (rigidbody.velocity.y > 0)
				rigidbody.velocity.y = 0;
	if (hitTrigger.transform.tag == "StairGoingDown")
		if (!Input.GetButton("Jump") && Vector3.Angle(rigidbody.velocity, hitTrigger.transform.forward) < 90)
			rigidbody.AddForce(0,-100,0);


	var current : GunScript = null;
	if (currentGun)
		current = currentGun.GetComponent(GunScript);
	var ammo : AmmoPickupScript = null;
	var gun : GunScript = null;
	if (hitTrigger.tag == "AmmoPickup" && waitToPickupAmmo <= 0)
	{
		ammo = hitTrigger.gameObject.GetComponent(AmmoPickupScript);
		if (current.currentExtraAmmo < current.maxExtraAmmo)
		{
			if (ammo.fromGun)
			{
				gun = ammo.gun.GetComponent(GunScript);
				if (gun.currentExtraAmmo > 0 && gun.ammoType == current.ammoType && ammo.gun != currentGun)
				{
					if (gun.currentExtraAmmo >= current.maxExtraAmmo - current.currentExtraAmmo)
					{
						gun.currentExtraAmmo -= current.maxExtraAmmo - current.currentExtraAmmo;
						current.currentExtraAmmo = current.maxExtraAmmo;
					}
					if (gun.currentExtraAmmo < current.maxExtraAmmo - current.currentExtraAmmo)
					{
						current.currentExtraAmmo += gun.currentExtraAmmo;
						gun.currentExtraAmmo = 0;
					}
					if (ammo.pickupSound)
						ammo.gameObject.GetComponent(AudioSource).Play();
				}
			}
			if (!ammo.fromGun)
			{
				if (current.ammoType == ammo.ammoType || ammo.ammoType == -1)
				{
					if (ammo.ammoAmount > 0 && !ammo.unlimitedAmmo)
					{
						if (ammo.ammoAmount >= current.maxExtraAmmo - current.currentExtraAmmo)
						{
							ammo.ammoAmount -= current.maxExtraAmmo - current.currentExtraAmmo;
							current.currentExtraAmmo = current.maxExtraAmmo;
						}
						if (ammo.ammoAmount < current.maxExtraAmmo - current.currentExtraAmmo)
						{
							current.currentExtraAmmo += gun.currentExtraAmmo;
							ammo.ammoAmount = 0;
						}
						if (ammo.pickupSound)
							ammo.gameObject.GetComponent(AudioSource).Play();
					}
					if (ammo.unlimitedAmmo)
					{
						current.currentExtraAmmo = current.maxExtraAmmo;
						if (ammo.pickupSound)
							ammo.gameObject.GetComponent(AudioSource).Play();
					}
				}
			}
		}
	}
}
function OnGUI(){
if(Event.current.type.Equals(EventType.repaint)){
var box : Rect = new Rect (x,y,w,h);
Graphics.DrawTexture(box,texture1,mat);
}
}

Ok this doesn’t include all the ai scripts or weapon scripts or the other like 20+ scripts i wrote.Please this project is 2 months in i can’t fix this i really need help!

It’s really hard for anyone else to pinpoint the source of your slow-running code - especially if you’ve been making it for 2 months! You need to learn to use the profiler or, failing that, disable parts of your code until you notice an appreciable different. (At what point did your game start running slowly? I’d begin by looking at the thing that you last added prior to that point).

Some general comments:

  • Why are you calling transform.parent.GetComponent(PlayerMovementScript) every Update loop? Presumeably, this won’t change throughout the duration of the game, so just get the component once (in Start, for example), and then re-use that variable in Update.

  • Don’t change physics components (i.e. rigidbody) in Update() or LateUpdate() - do them in FixedUpdate(). And don’t do anything in FixedUpdate() other than physics things. And don’t set rigidbody.velocity directly at all.

  • Don’t use OnGUI() at all if you can help it. It always kills performance.

  • Where you have a series of mutually exclusive “if” tests, make them into “else ifs”. e.g. If the hit trigger is tagged “StairGoingUp”, we really don’t need to also test whether it is tagged “StairGoingDown”.

I don’t think those alone will explain a performance quite as bad as you describe, but they’ll improve it a bit, and that’s all we can go on from the snippets you’ve posted.