Hi folks. In need of an assist.
Wrapping up project boost and discovered that my first build was completely buggy when applying and testing on my browser.
-
Collisions are disabled despite the build settings “Development Build” checkbox not checked
-
My Debug keys ‘C’ & ‘L’ however remain disabled.
-
My Rocket rotates on the y & x axis despite Freeze Rotation for both are enabled.
-
The framerate of gameplay is just plain awful. I expected a drop in framerate but from what I am witnessing is just abysmal.
- My audio, rotate on Z axis, and thrust space key) remains intact!
I went as far as combing through my code countless times and clearing my browser’s history/cache just for giggles and laughs.
Any suggestions would be great. Thanks!
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour {
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip rocketDeath;
[SerializeField] AudioClip goalJingle;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem rocketDeathParticles;
[SerializeField] ParticleSystem goalJingleParticels;
Rigidbody rigidBody;
AudioSource audioSource;
enum State { Alive, Dying, Transcending }
State state = State.Alive;
bool collisionsDisabled = false;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update ()
{
if (state == State.Alive)
{
RespondToThrustInput();
ResondToRotateInput();
}
if (Debug.isDebugBuild)
{
RespondToDebugKeys();
}
}
private void RespondToDebugKeys()
{
if (Input.GetKeyDown(KeyCode.L))
{
LoadNextLevel();
}
else if (Input.GetKeyDown(KeyCode.C))
{
collisionsDisabled = !collisionsDisabled;
}
}
void OnCollisionEnter(Collision collision)
{
if(state != State.Alive || !collisionsDisabled) { return; }
switch (collision.gameObject.tag) //ignore collisions when dead
{
case "Friendly":
//do nothing
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(goalJingle);
goalJingleParticels.Play();
Invoke("LoadNextLevel", levelLoadDelay);
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(rocketDeath);
rocketDeathParticles.Play();
Invoke("LoadFirstLevel", levelLoadDelay);
}
private void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0; //loop back to start
}
SceneManager.LoadScene(nextSceneIndex);
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime); //odd behavior..
if (!audioSource.isPlaying) // so it doesnt layer
{
audioSource.PlayOneShot(mainEngine);
}
mainEngineParticles.Play();
}
private void ResondToRotateInput()
{
rigidBody.freezeRotation = true; // take manual control over rotation
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false; // resume physics control of rotation
}
}