using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour {
[SerializeField]float rcsThrust = 100f; // makes switches for your game
[SerializeField] float mainThrust = 100f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip success;
[SerializeField] AudioClip death;
[SerializeField] float levelLoad = 1f;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem successParticles;
[SerializeField] ParticleSystem deathParticles;
Rigidbody rb; // fucntion to var
AudioSource audioSource;
enum State { Alive, Dying, Transcending}
State state = State.Alive;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>(); // calling out the componets
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update ()
{
if (state == State.Alive)
// todo stop sound somewhere
{
RespondToRotate();
RespondToThrust();
}
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; } /// ignores collision when dead
switch (collision.gameObject.tag) // tells the game....
{
case "Finish":
StartSuccess();
break;
case "Friendly":
// if the tag says Friendly, say OK
print("OK");
break;
case "Enemy":
// if the tag says enemy, say you died
StartDeath();
break;
}
}
private void StartDeath()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
deathParticles.Play();
Invoke("LoadFirstLevel", levelLoad);
}
private void StartSuccess()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
successParticles.Play();
Invoke("LoadNextLevel", levelLoad);
}
private void LoadNextLevel()
{
SceneManager.LoadScene(1); // allow for more than 2 levels
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
void RespondToRotate()
{
rb.freezeRotation = true; // take manual control over rotation
float rotationThisFrame = rcsThrust * Time.deltaTime; // time delta is FPS
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rb.freezeRotation = false; // resume physics control
}
void RespondToThrust()
{
ApplyThrust();
}
private void ApplyThrust()
{
float ThrustThisFrame = mainThrust * Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
rb.AddRelativeForce(Vector3.up * ThrustThisFrame);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
} // so it doen't layer
mainEngineParticles.Play();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
}
The code works, the only problem is when i enter in unity and play the game. If i hit a wall, you are supposed to see explosion particles. Instead they only play for a split second and restart my level.
Thanks in advance for any help