Thank God this was here, my brain being slow, thank so much for the help
OMG Thanks!
You sire are a saviour! I was going mental trying to figure what I was doing wrong!! Thank you so much!
Thank you! This helped me tonight!
Thank you, this worked for me. It’s a very strange workaround but it did just as you said. I do notice it doesn’t work on Level 2 yet but I am thinking that will be amended later because it’s a different scene.
This also worked once I moved the .Play and .Stop into the if and else of the ApplyThrust and RespondToThrust respectively. I also had to make sure that the particle prefabs were dragged into the scene hierarchy under the gameObject and then applied to the serialized fields from the Hierarchy and not the Project Tab. Here is my code below.
using System;
using System.Collections;
using System.Collections.Generic;
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;
// 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();
Rotate();
}
}
void OnCollisionEnter(Collision collision)
{
if (state != State.Alive) { return; } // Ignore Collision
switch (collision.gameObject.tag)
{
case "Friendly":
// do nothing
break;
case "Finish":
StartWinSequence();
break;
default:
StartDeathSequnce();
// kill player
break;
}
}
private void StartWinSequence()
{
state = State.Transcending;
audioSource.Stop();
audioSource.PlayOneShot(success);
successParticles.Play();
Invoke("LoadNextLevel", 1f);
}
private void StartDeathSequnce()
{
state = State.Dying;
audioSource.Stop();
audioSource.PlayOneShot(death);
deathParticles.Play();
Invoke("LoadFirstLevel", 1f);
}
private void LoadNextLevel()
{
SceneManager.LoadScene(1); //TODO Allow more levels
}
private void LoadFirstLevel()
{
SceneManager.LoadScene(0);
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.Space)) // can thrust while rotating
{
ApplyThrust();
}
else
{
audioSource.Stop();
mainEngineParticles.Stop();
}
}
private void ApplyThrust()
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (!audioSource.isPlaying) // so it doesn't layer
{
audioSource.PlayOneShot(mainEngine);
mainEngineParticles.Play();
}
}
private void Rotate()
{
rigidBody.freezeRotation = true; // take manual control of 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
}
}
Thanks for posting, this saved me a lot of time.
Could this be down to the updated way Unity is handling prefabs in the newer versions?
Best
L