using UnityEngine.SceneManagement;
using UnityEngine;
public class Rocket : MonoBehaviour
{
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 30f;
[SerializeField] AudioClip MainEngine;
[SerializeField] AudioClip OnDeath;
[SerializeField] AudioClip NextLevel;
[SerializeField] float LevelLoadDelay = 1f;
[SerializeField] ParticleSystem MainEngineParticles;
[SerializeField] ParticleSystem OnDeathParticles;
[SerializeField] ParticleSystem NextLevelParticls;
Rigidbody rigidBody;
AudioSource audiosource;
enum State { Alive, Dying, Transcending}
State state = State.Alive;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
audiosource = GetComponent<AudioSource>();
}
void Update()
{
if (state == State.Alive)
{
RespondToThrust();
RespondToRotation();
}
}
private void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; } //Stop the execution
switch (collision.gameObject.tag)
{
case "Friendly":
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartDeathSequence();
break;
}
}
private void StartSuccessSequence()
{
state = State.Transcending;
audiosource.Stop();
audiosource.PlayOneShot(NextLevel);
NextLevelParticls.Play();
Invoke("LoadNextScene", LevelLoadDelay);
print("Pass");
}
private void StartDeathSequence()
{
state = State.Dying;
audiosource.Stop(); //To stop the current sound
audiosource.PlayOneShot(OnDeath);
OnDeathParticles.Play();
Invoke("LoadFirstLevel", LevelLoadDelay);
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
private void LoadNextScene()
{
SceneManager.LoadScene(1);//todo allow for more than 2 levels.
}
private void RespondToRotation()
{
ApplyRotation();
}
private void ApplyRotation()
{
float rotationThisFrame = rcsThrust * Time.deltaTime;
rigidBody.freezeRotation = true;
if (Input.GetKey(KeyCode.A))
{
print("Left");
transform.Rotate(Vector3.forward * rotationThisFrame);
}
else if (Input.GetKey(KeyCode.D))
{
print("Right");
transform.Rotate(-Vector3.forward * rotationThisFrame);
}
rigidBody.freezeRotation = false;
}
private void RespondToThrust()
{
if (Input.GetKey(KeyCode.Space))
{
ApplyThrust();
}
else
{
audiosource.Stop();
MainEngineParticles.Stop();
}
}
private void ApplyThrust()
{
float ThrustThisFrame = mainThrust * Time.deltaTime;
rigidBody.AddRelativeForce(Vector3.up * ThrustThisFrame );
if (!audiosource.isPlaying)
{
audiosource.PlayOneShot(MainEngine);
}
MainEngineParticles.Play();
}
}
after multiplying Time.deltaTime in my Thrust , my rocket stops flying Why?
though my thrust partical system is working fine