I was going along fine and then … something happened? I was moving things around and now it’s giving me a CS1022 error. I’m sure it’s just a stupid syntax error, misplaced bracket, or something not capitalized but I can’t for the life of me figure out what’s wrong with it.
Here is the code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour
{
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip success;
[SerializeField] AudioClip death;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem successParticles;
[SerializeField] ParticleSystem deathParticles;
Rigidbody rigidBody;
AudioSource audioSource;
enum State { Alive, Dying, Transcending }
State state = State.Alive;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
// to do somewhere stop sound on death
if (state == State.Alive)
{
RespondToThrustInput();
RespondToRotateInput();
}
}
void OnCollisionEnter(Collision collision)
{
if (state !=State.Alive) { return; } //ignore collions when dead
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
Invoke("LoadNextLevel", 1f); // parameterise time
}
private void StartDeathSequence()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
Invoke("LoadFirstLevel", 1f); // parameterise time
}
private void LoadNextLevel()
{
SceneManager.LoadScene(1); // Allow for more than 2 levels
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0); // Allow for more than 2 levels
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space)) // Thrust while rotating
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying) //so the audio doesn't layer on top of itself
{
audioSource.PlayOneShot(mainEngine);
mainEngineParticles.Play(mainEngineParticles);
}
}
private void RespondToRotateInput()
{
rigidBody.freezeRotation = true; // take manual control of rotation
float rcsThrust = 100f;
float rotationThisFrame = rcsThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.A))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false; // resume physics control of rotation
}
}
Any help would be appreciated! Thanks!