Reputation: 31
I made a Project in Unity, a game to be clear. So the player has a Sci-fi car and he tries to avoid obstacles-rocks. When the car hits a rock, the Game Manager load the "credits" screen to choose between quit or restart. My problem is that I want to make the rock explode when the car goes on it and then the Game Manager will load the "credits" screen.
Some of my code:
For player collision:
public class PlayerCollision : MonoBehaviour {
public PlayerMovement movement;
public static bool y = true;
public void OnCollisionEnter (Collision collisionInfo)
{
// We check if the object we collided with has a tag called "Obstacle".
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false; // Disable the players movement.
y = false;
FindObjectOfType<GameManager>().EndGame();
}
}
}
Game Manager:
public class GameManager : MonoBehaviour {
bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel ()
{
completeLevelUI.SetActive(true);
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene("Credits");
//SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
The endTrigger:
public class EndTrigger : MonoBehaviour {
public GameManager gameManager;
void OnTriggerEnter ()
{
gameManager.CompleteLevel();
}
}
LevelComplete:
public class LevelComplete : MonoBehaviour {
public void LoadNextLevel ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
Menu:
public class Menu : MonoBehaviour {
public void StartGame ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
I have written other scripts. If you want anything let me know. Thanks guys.
Upvotes: 0
Views: 7804
Reputation: 7605
I would change your approach. As I understand you already have an endTrigger [With a Collider Trigger] that ends the game if the player goes inside. So uncheck the trigger in the player collider and instead add that trigger to the rocks collider, so when the player crashes it will:
You can add this script to your rock and see how it works
public class RockTrigger : MonoBehaviour {
public GameManager gameManager;
ParticleSystem myParticleSystem;
void Awake()
{
myParticleSystem = GetComponent<ParticleSystem>();
}
void OnTriggerEnter(Collider other)
{
myParticleSystem.Play();
GetComponent<MeshRenderer>.enabled = false;
gameManager.GetComponent<GameManager>().EndGame();
}
}
Note about the OnTriggerEnter: In case you only have the player moving in the scene this is fine like this (I use the same pattern you used in EndGame),
but in case there were other GameObject with a RigidBody moving in the
scene you should check if other
is indeed the player. Usually tagging the player and the checking if(other.tag == "Player")
Upvotes: 1
Reputation: 4343
Add a particle system to your rocks. Uncheck "looping" and "play on awake". Then you can play by adding the line below.
public void OnCollisionEnter (Collision collisionInfo)
{
// We check if the object we collided with has a tag called "Obstacle".
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false; // Disable the players movement.
y = false;
collisionInfo.gameObject.GetComponent<ParticleSystem>().Play(); // play the explosion
FindObjectOfType<GameManager>().EndGame();
}
}
Upvotes: 1