I was building a scene with only one script that was used for controlling the player. The game was running at 70 fps. The next day, I loaded it all up and I was running at 50 fps. I’ve tried disabling scripts, checking scripts for memory issues, or just bad programming, and minimized my colliders. Nothing I change seems to be making a big difference in my fps. I thought it might be my objects, but there’s just a big room with six tunnels, all with very minimal meshes (less thaan 850 verts for the biggest room and less than 80 for the tunnels). I’m using three materials, two of which are just diffuse solid colors, and the third is a normal mapped wood flooring texture. I do have small ceiling lights, with less than 20 verts, and lights on each. I did have 36 lights, but I grouped some together so that it provided the same light with less lights in the scene since i know that can definitely cause performance issues. I even tried disabling the lights, dynamic shadows, all that fun stuff, and it doesn’t change. I’m not sure what to do at this point. Anyone have any tips or things I’m not thinking about?
Edit: So here’s the scripts that I’m running:
using UnityEngine;
using System.Collections;
public class PlayerManager : MonoBehaviour {
public float weight;
public float moveSpeed;
public float rotateSpeed;
public float vertRotateSpeed;
public float maxVelocity;
public float playerHeight;
public GameObject mainCamera;
public GameObject cameraRotator;
public float lerpPlayerYSpeed;
public float yDistanceSpatial;
public float maxVertAngle;
public float minVertAngle;
private float playerY;
void FixedUpdate() {
if(GameManager.gameMode == GameMode.playable) {
Ray rayDown = new Ray (gameObject.transform.position, Vector3.down);
RaycastHit hit;
Vector3 target = new Vector3 (rayDown.origin.x, rayDown.origin.y - playerHeight - yDistanceSpatial, rayDown.origin.z);
Debug.DrawLine (rayDown.origin, target);
if (Physics.Raycast (rayDown, out hit, playerHeight + yDistanceSpatial)) {
gameObject.rigidbody.useGravity = false;
gameObject.rigidbody.velocity = new Vector3(gameObject.rigidbody.velocity.x, 0.0f, gameObject.rigidbody.velocity.z);
playerY = hit.point.y + playerHeight;
lerpPlayerPosY ();
if(hit.rigidbody != null) {
hit.rigidbody.AddForceAtPosition(Vector3.down * weight, hit.point);
}
} else {
gameObject.rigidbody.useGravity = true;
}
cameraRotator.transform.Rotate(new Vector3(0.0f, Time.deltaTime * rotateSpeed * Input.GetAxis("Mouse X"), 0.0f));
float vertDistance = Time.deltaTime * -vertRotateSpeed * Input.GetAxis("Mouse Y");
if (mainCamera.transform.eulerAngles.x < 180) {
if(mainCamera.transform.eulerAngles.x + vertDistance < minVertAngle) {
mainCamera.transform.Rotate(new Vector3(vertDistance, 0.0f, 0.0f));
}
} else {
if(mainCamera.transform.eulerAngles.x + vertDistance > maxVertAngle) {
mainCamera.transform.Rotate(new Vector3(vertDistance, 0.0f, 0.0f));
}
}
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) {
gameObject.rigidbody.AddForce(cameraRotator.transform.forward * moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) {
gameObject.rigidbody.AddForce(cameraRotator.transform.forward * -moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
gameObject.rigidbody.AddForce(cameraRotator.transform.right * -moveSpeed * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
gameObject.rigidbody.AddForce(cameraRotator.transform.right * moveSpeed * Time.deltaTime);
}
Vector3 newV = gameObject.rigidbody.velocity;
if (Mathf.Abs (newV.x) > maxVelocity || Mathf.Abs (newV.z) > maxVelocity) {
newV.x = Mathf.Clamp (newV.x, -maxVelocity, maxVelocity);
newV.z = Mathf.Clamp (newV.z, -maxVelocity, maxVelocity);
gameObject.rigidbody.velocity = newV;
}
}
}
void lerpPlayerPosY() {
Vector3 pos = gameObject.transform.position;
pos.y = Mathf.Lerp (pos.y, playerY, Time.deltaTime * lerpPlayerYSpeed);
gameObject.transform.position = pos;
}
}
GameManager:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameManager : MonoBehaviour {
public float refreshInt = 1.0f;
public GameObject fpsCounter;
public static float last_FPS = 0;
private float fps = 0;
private float time_Passed = 0.0f;
public static GameMode gameMode;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
Screen.showCursor = false;
gameMode = GameMode.playable;
}
void Update() {
time_Passed += Time.deltaTime;
if (time_Passed > refreshInt) {
fps *= (1.0f/refreshInt);
last_FPS = fps;
fps = 0;
time_Passed -= refreshInt;
fpsCounter.GetComponent<Text>().text = "FPS: " + last_FPS;
} else {
fps++;
}
if(Input.GetKey(KeyCode.Escape)) {
Application.Quit();
}
if (Input.GetKeyDown (KeyCode.T)) {
if(fpsCounter.activeSelf) {
fpsCounter.SetActive(false);
} else {
fpsCounter.SetActive(true);
}
}
if(Input.GetKey(KeyCode.Space)) {
CrossFading.FadeIn(5.0f);
}
if (CrossFading.isDone ()) {
CrossFading.FadeOut(4.0f);
}
if(Input.GetKeyDown(KeyCode.G)) {
if(gameMode == GameMode.playable) {
gameMode = GameMode.cutScene;
} else {
gameMode = GameMode.playable;
}
}
}
}
Some of the stuff in the above script is just testing the behavior of different things that are set up. Like the fading in and out before a cutscene, or toggling fps. Which by the way, the code I have doesn’t seem to give me accurate fps, not sure why.
And lastly, CrossFading:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CrossFading : MonoBehaviour {
public static float speed = 0.5f;
private static bool faded = true;
private static float targetAlpha;
private static Color currColor;
void Start() {
currColor = gameObject.GetComponent<Image> ().color;
targetAlpha = currColor.a;
}
void Update() {
if (!isDone ()) {
currColor.a = Mathf.Lerp (currColor.a, targetAlpha, speed * Time.deltaTime);
gameObject.GetComponent<Image> ().color = currColor;
}
}
public static void FadeIn(float newSpeed) {
faded = false;
speed = newSpeed;
targetAlpha = 1.0f;
}
public static void FadeOut(float newSpeed) {
faded = true;
newSpeed = newSpeed;
targetAlpha = 0.0f;
}
public static bool isDone() {
float a = currColor.a;
if (faded) {
if(a < 0.001f) {
return true;
} else {
return false;
}
} else {
if(a > 0.999f) {
return true;
} else {
return false;
}
}
}
}
I reduced the quality settings and now its up to about 50 fps. But I still feel like it should be better. Screen shots of scene: