Added in level-specific restart code!

Unlike the challege in terminal hacker, I was able to figure this out by myself! This time though, I knew what would solve my problem (finding a way to get the scene name and acting on it) and was able to find it online. I believe this practice can be encouraged in the challenges in the course by giving a bit more clues as to what to search for (by describing the problem for us): Anyway, here is my code (i know a new method is introduced to handle this in the next lesson, but was proud and had to share :slight_smile:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    Rigidbody rigidBody;
    AudioSource audioData;
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 10f;
    string sceneName;


    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioData = GetComponent<AudioSource>();
        sceneName = SceneManager.GetActiveScene();
        print(sceneName);
    }

    // Update is called once per frame
    void Update()
    {
        Thrust();
        Rotate();
    }

    private void Thrust()
    {
        if (Input.GetKey(KeyCode.Space))// can thrust while rotating
        {
            rigidBody.AddRelativeForce(mainThrust * Vector3.up);
            if (!audioData.isPlaying) //so it doesn't layer
            {
                audioData.Play();
            }
        }
        else
        {
            audioData.Stop();
        }
    }

    private void Rotate()
    {
        float rotationThisFrame = rcsThrust * Time.deltaTime;
        rigidBody.freezeRotation = true; //take manual control of the rotation
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(rotationThisFrame * Vector3.forward);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(rotationThisFrame * -Vector3.forward);
        }
        rigidBody.freezeRotation = false; //resume physics control of the rotation
    }

    void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "Friendly":
                print("Friend");
                break;

            case "Finish":
                print("Finish");
                if (sceneName == "Level 1")
                {
                    SceneManager.LoadScene(1);
                }
                else if (sceneName == "Level 2")
                {
                    print("Congrats!");
                }
                break;

            default:
                print("Dead, level is restarting");
                if (sceneName == "Level 1")
                {
                    SceneManager.LoadScene(0);
                    print("Starting " + sceneName);
                }
                else if (sceneName == "Level 2")
                {
                    SceneManager.LoadScene(1);
                    print("Starting " + sceneName);
                }

                break;
        }
    }
}
2 Likes

I had problems with the string definition but I managed to solve it that way (+ when you die in level 2 you dont get looped back to 1). Will just post it in addition if a reloop to 1 isnt desired.

Thanks a lot for the input

public class FlyingInput : MonoBehaviour
{
 
    int sceneNumber;

public void Start()
    {
       
        sceneNumber = SceneManager.GetActiveScene().buildIndex;
       }

int currentScene = SceneManager.GetActiveScene().buildIndex;
public void OnCollisionEnter(Collision collision)
    {
        
        switch (collision.gameObject.tag)
        {
            case "Friendly":
                print("Friend");

                break;

            case "Final":

                if (sceneNumber == 1)

                {
                    SceneManager.LoadScene(0);
                }

                else if (sceneNumber == 0)
                {
                    SceneManager.LoadScene(1);
                }

                break;

                default:

                    if (sceneNumber == 0)
                    {
                        SceneManager.LoadScene(0); }

                    else if (sceneNumber == 1) { 
                { SceneManager.LoadScene(1); }
                }
                    break;
                }
    }

Privacy & Terms