I followed along and added in the code just like Ben did but my Level 2 will not load when my rocket hits the landing pad. It will flash like it is going to load but instead it goes back to the launch pad for Level 1.
~
using UnityEngine;
using UnityEngine.SceneManagement;
public class Rocket : MonoBehaviour {
[SerializeField] float rcsThrust = 100f;
[SerializeField] float mainThrust = 100f;
Rigidbody rigidBody;
AudioSource audioSource;
// Use this for initialization
void Start () {
rigidBody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
Thrust();
Rotate();
}
void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
// do nothing
break;
case "Finish":
print("Hit finish"); // todo remove
SceneManager.LoadScene(1);
break;
default:
print("Dead");
SceneManager.LoadScene(0);
break;
}
}
private void Thrust()
{
if (Input.GetKey(KeyCode.Space))// can thrust while rotating
{
rigidBody.AddRelativeForce(Vector3.up * mainThrust);
if (audioSource.isPlaying == false) //so it doesn't layer
{
audioSource.Play();
}
}
else
{
audioSource.Stop();
}
}
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
}
}
~