Hi everyone !
Before I add the code in this course, everything work well :
- Rotation stopped after collision
- Sound work normally for thrust and collision
After I add the code in this course :
- Rotation still working
- Sound of Thrust don’t stop
- Sound on collision don’t work anymore, but need to be triggered multiples times before to work.
I tried to look at before courses to see if I missed something and also tried to copy the code on this course. Even tried to restart my unity but still doesn’t work.
Can someone help me ?
CollisionHandler script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
AudioSource audioSource;
[SerializeField] float levelLoadDelay = 1.5f;
[SerializeField] AudioClip crash;
[SerializeField] AudioClip success;
bool isTransitioning = false;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
if (isTransitioning)
{
return;
}
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("This is friendly");
break;
case "Finish":
StartFinishSequence();
break;
default:
StartCrashSequence();
break;
}
}
void StartFinishSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(success);
GetComponent<Movement>().enabled = false;
Invoke("LoadNextLevel", levelLoadDelay);
}
void StartCrashSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(crash);
GetComponent<Movement>().enabled = false;
Invoke("ReloadScene", levelLoadDelay);
}
void ReloadScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
}
Movement script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class Movement : MonoBehaviour
{
Rigidbody myRigidbody;
[SerializeField] float moveSpeed = 100f;
[SerializeField] float rotationSpeed = 10f;
[SerializeField] AudioClip mainThrust;
AudioSource audioSource;
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
ProcessThrust();
RotationThrust();
}
void ProcessThrust()
{
if (Input.GetKey(KeyCode.Space))
{
myRigidbody.AddRelativeForce(Vector3.up * Time.deltaTime * moveSpeed);
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(mainThrust);
}
}
else
{
audioSource.Stop();
}
}
void RotationThrust()
{
if(Input.GetKey(KeyCode.Q))
{
ApplyRotation(rotationSpeed);
}
else if(Input.GetKey(KeyCode.D))
{
ApplyRotation(-rotationSpeed);
}
}
void ApplyRotation(float RotateMe)
{
myRigidbody.freezeRotation = true;
transform.Rotate(Vector3.forward * Time.deltaTime * RotateMe);
myRigidbody.freezeRotation = false;
}
}