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
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;
}
}
}