At a genuine loss. Debugs work, audio work on thrust only, which is apart of the Movement script
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionBehavior : MonoBehaviour
{
[SerializeField] float holdLevel;
[SerializeField] AudioClip youWin;
[SerializeField] AudioClip youLose;
AudioSource audioSource;
private void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("Friendly");
break;
case "Fuel":
Debug.Log("More Fuel");
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartCrashSequence();
break;
}
}
//When Player Wins
void StartSuccessSequence()
{
Debug.Log("But have you won?");
Invoke("NextLevel", holdLevel);
audioSource.PlayOneShot(youWin);
}
//When Player Crashs
void StartCrashSequence()
{
Debug.Log("You've Crashed");
audioSource.PlayOneShot(youLose);
Invoke("ReloadLevel", 10f);
}
// If crashed reloads scene from Unity Build through index numbers
void ReloadLevel()
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentScene);
}
// If succeed loads scene scene using Unity Build index number. Adds 1 from current scene.
void NextLevel()
{
int currentScene = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentScene + 1);
}
}
Here is also the Movement Script.
using UnityEngine;
public class Movement : MonoBehaviour
{
[SerializeField] float mainThrust = 100;
[SerializeField] float mainRotation = 100;
[SerializeField] AudioClip mainEngine;
Rigidbody rb;
AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = rb.GetComponent<AudioSource>();
}
// Method ProcessInput THRUST
void ProcessInput()
{
if (Input.GetKey(KeyCode.Space))
{
Debug.Log(" You have pressed Thurster ");
rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainEngine);
}
}
else
{
audioSource.Stop();
}
}
// Method ProcessRotation ROTATION
void ProcessRotation()
{
if (Input.GetKey(KeyCode.D))
{
ApplyRotation(mainRotation);
}
else if (Input.GetKey(KeyCode.A))
{
ApplyRotation(-mainRotation);
}
}
void ApplyRotation( float mainRotation)
{
rb.freezeRotation = true;
transform.Rotate(Vector3.back * mainRotation * Time.deltaTime);
rb.freezeRotation = false;
}
//Updates everyframe
void Update()
{
ProcessInput();
ProcessRotation();
rb.mass = .05f;
}
}
.