I’ve been struggling with this issue for five days straight, without taking a break. Just as I get close to fixing the problem, it reoccurs! Let me describe the problem I’m facing:
The main element in my scenario is a plane, which represents my character. After analyzing the profiler, it’s evident that the player is the most critical aspect causing disruptions in the game. Additionally, the profiler also indicates the editor utilizing a significant amount of RAM and resources, leading me to consider Unity as a potential factor. Unfortunately, my attempt to resolve the issue by creating a build didn’t yield positive results – in short, it didn’t work.
Initially, my game was progressing smoothly without any problems. My plan was on track, with only the addition of sounds, visual effects (FX), user interface (UI), and graphics left to complete. I began by implementing sound effects. While the rocket explosion sound functioned for about a minute, it subsequently triggered a game crash. I dedicated two entire days to addressing this issue, and I managed to resolve it. I felt a sense of accomplishment, which motivated me to tackle the remaining sound effects. However, this optimism was short-lived, as the crashes persisted. This repetitive cycle of opening Unity, making adjustments, testing, encountering crashes (occasionally resulting in Unity crashing and even affecting my PC if left open), and repeating the process has left me extremely frustrated. Dealing with this ordeal is causing a decline in my sanity.
I’m desperately seeking solutions, advice, or any insights you might have. My current approach involves sifting through numerous articles and delving into scripting APIs, which is proving to be mentally taxing.
While occasional crashes related to enemy entities have been observed, they are relatively manageable and can often be resolved. However, the real challenge lies in addressing the errors within the player script. Surprisingly, Visual Studio isn’t indicating any issues.
Regarding performance, the FPS (frames per second) in the profiler used to stay consistently between 1000 and 1010 FPS. Upon adding the rocket explosion, the FPS dropped to a range of 100 to 120, which was still acceptable. However, after implementing the “ucakdus” sound, FPS plummeted to values between 15, 30, and occasionally 60, with frequent spikes within the 15-30 range. These spikes eventually lead to a complete freeze of the game, accompanied by a system crash.
HERE IS THE CODE:
using System.Collections;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioSource propellerSound;
public AudioSource gameMusic;
private GameManager gameManager;
public Camera Camera;
public GameObject plane;
public GameObject propeller;
[SerializeField] private Rigidbody rb;
public GameObject algila; //ALGILA means DETECT
public Vector3 rotation;
public Vector3 worldPos;
public Vector3 offset;
public Vector3 currentVelocity;
public int health;
public float horizontalInput;
public float verticalInput;
public float count = 0;
[SerializeField] private float speed;
public float currentSpeed;
[SerializeField] private float turningSpeed = 0.5f;
public float smoothTime = 0.25f;
public float cameraSpeed;
public float currentAmmo = 250;
private bool isHoldingW = false;
private bool isHoldingSpace = false;
private bool isHoldingR = false;
public bool rotationAlright = false;
public bool movePlaneForward = false;
public bool dashHappened = false;
public bool canMove = true;
public bool useAlternativeControls;
public Material vinyet;
public GameObject spawnPlace1;
public GameObject spawnPlace2;
public GameObject spawnPlace3;
public GameObject spawnPlace4;
public GameObject rocketSpawnPlace1;
public GameObject rocketSpawnPlace2;
public GameObject bullet1;
public GameObject rocket1;
public GameObject bulletParent;
public GameObject rocketParent;
public AudioSource mermiSes;
public CheckFront checkFront;
public float standartTimeRemaining = 2;
public float timeRemaining;
private int whichSideRocketSpawn = 1;
private int bulletCount;
private int rocketCount;
public bool shouldSpawn;
public bool shouldRocketSpawn;
public AudioSource ucakDus;
//private bool motorOverClocked = false;
private void Start()
{
gameManager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
rb = GetComponent<Rigidbody>();
ConfinedCursor();
Camera = Camera.main;
shouldSpawn = false;
timeRemaining = standartTimeRemaining;
checkFront = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<CheckFront>();
}
private void Update()
{
if (this != null)
{
//Debug.Log(GameObject.FindGameObjectWithTag("Bina").name);
horizontalInput = Input.GetAxis("HorizontalAD");
verticalInput = Input.GetAxis("VerticalW");
float rotateHorizontal = Input.GetAxis("Mouse X");
float rotateVertical = Input.GetAxis("Mouse Y");
float alternativeRotateHorizontal = Input.GetAxis("ArrowsLeftRight");
float alternativeRotateVertical = Input.GetAxis("ArrowsUpDown");
currentSpeed = Mathf.RoundToInt(rb.velocity.magnitude * 2.237f);
propeller.transform.Rotate(0, 0, currentSpeed);
propellerSound.volume = 0.5f;
if (isHoldingW)
{
propellerSound.volume += Time.deltaTime * 6;
propellerSound.loop = true;
}
else
{
propellerSound.volume -= Time.deltaTime;
propellerSound.loop = false;
}
if (Input.GetKeyDown(KeyCode.Q))
{
ConfinedCursor();
}
if (Input.GetKeyDown(KeyCode.E))
{
EscapedMouse();
}
if (Input.GetKeyDown(KeyCode.N))
{
useAlternativeControls = !useAlternativeControls;
}
if (gameManager.gameIsPlayable)
{
transform.Rotate(rotation * turningSpeed);
transform.Rotate(0, horizontalInput * turningSpeed, 0);
isHoldingW = Input.GetKey(KeyCode.W);
if (isHoldingW && currentSpeed < 200 && canMove)
{
move();
}
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
if (!dashHappened)
{
dash();
//Debug.Log("Dash happened!");
}
}
if (dashHappened)
{
StartCoroutine(DashCooldown());
}
isHoldingSpace = Input.GetKey(KeyCode.Space);
if (isHoldingSpace)
{
shouldSpawn = true;
}
else
{
shouldSpawn = false;
}
isHoldingR = Input.GetKeyDown(KeyCode.R);
//This will detect clicking not holding, I will fix it's name later.
if (isHoldingR)
{
shouldRocketSpawn = true;
}
else
{
shouldRocketSpawn = false;
}
if (rotationAlright)
{
if (useAlternativeControls)
{
rotation = new Vector3(alternativeRotateHorizontal * 2, 0.0f, alternativeRotateVertical * 2);
}
else
rotation = new Vector3(rotateHorizontal, 0.0f, rotateVertical);
}
else
{
rotation = Vector3.zero;
}
}
}
Quaternion rotateCorrect = new Quaternion(this.transform.rotation.x, this.transform.rotation.y, this.transform.rotation.z, this.transform.rotation.w);
Quaternion rotateCorrectRocket = new Quaternion(this.transform.rotation.x, this.transform.rotation.y - 93, this.transform.rotation.z, this.transform.rotation.w);
/*if (algila.GetComponent<Algila>().timeHasSlowed)
{
mermiSes.pitch = 0.5f;
}
else
{
mermiSes.pitch = 1.0f;
}*/
bulletCount = GameObject.FindGameObjectsWithTag("Bullet").Length;
if (shouldSpawn && bulletCount < 50 && this.rotationAlright)
{
Instantiate(bullet1, spawnPlace1.transform.position, rotateCorrect, bulletParent.transform);
Instantiate(bullet1, spawnPlace2.transform.position, rotateCorrect, bulletParent.transform);
Instantiate(bullet1, spawnPlace3.transform.position, rotateCorrect, bulletParent.transform);
Instantiate(bullet1, spawnPlace4.transform.position, rotateCorrect, bulletParent.transform);
}
rocketCount = GameObject.FindGameObjectsWithTag("Rocket").Length;
if (shouldRocketSpawn && rocketCount < 2 && this.rotationAlright)
{
if (whichSideRocketSpawn == 1)
{
Instantiate(rocket1, rocketSpawnPlace1.transform.position, rotateCorrectRocket, rocketParent.transform);
whichSideRocketSpawn = 0;
}
else
{
Instantiate(rocket1, rocketSpawnPlace2.transform.position, rotateCorrectRocket, rocketParent.transform);
whichSideRocketSpawn = 1;
}
}
if (movePlaneForward && this)
{
move();
}
//MOST PROBLEMATIC PLACE
if (health <= 0)
{
die();
}
//MOST PROBLEMATIC PLACE
}
private void move()
{
rb.AddRelativeForce(Vector3.left * speed * verticalInput);
//speed += Time.deltaTime /2;
}
private void dash()
{
rb.AddRelativeForce(Vector3.left * currentSpeed / 1.5f * verticalInput, ForceMode.Impulse);
dashHappened = true;
}
private void ConfinedCursor()
{
Cursor.lockState = CursorLockMode.Confined;
gameManager.gameIsPlayable = true;
Cursor.visible = false;
movePlaneForward = false;
}
private void EscapedMouse()
{
Cursor.lockState = CursorLockMode.None;
gameManager.gameIsPlayable = false;
Cursor.visible = true;
}
private IEnumerator DashCooldown()
{
yield return new WaitForSeconds(5);
//Debug.Log("Dash is ready!");
dashHappened = false;
}
//MOST PROBLEMATIC PLACE
private IEnumerator die()
{
ucakDus.Play();
if (!ucakDus.isPlaying)
{
yield return new WaitForSeconds(2);
gameManager.gameIsPlayable = false;
}
}
//MOST PROBLEMATIC PLACE
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground") && this != null)
{
rotationAlright = false;
}
else if (collision.gameObject.CompareTag("TESTPLACE") && this != null)
{
rotationAlright = true;
}
else if (collision.gameObject.CompareTag("EnemyRocket"))
{
health = health - 10;
Destroy(collision.gameObject);
}
else if (collision.gameObject.CompareTag("EnemyPlaneRocket"))
{
health = health - 10;
Destroy(collision.gameObject);
}
else if (collision.gameObject.CompareTag("EnemyBullet") && this != null)
{
health--;
}
else if (collision.gameObject.CompareTag("Enemy") && this != null)
{
gameObject.GetComponents<AudioSource>()[0].Play();
//Destroy(gameObject);
Destroy(collision.gameObject);
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground") && this != null)
{
rotationAlright = true;
}
}
/* private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyBullet") && this != null)
{
health--;
}
}*/
}