Player dies when hitting the floor

Hi

I am new to unity and coding, and i have started making a game, i have made some progress on it but i am having trouble finding some of my anwsers on youtube and the unity forum, and sometimes when i do, i still can’t get things to work. So this is what im trying to do.

I have a map and the player is on top of the tower, i want the player to fall and when hitting the ground, dies with it displaying game over, What could i do to make this happen and what script?

Thanks for the help

Hello!

  • Make sure your player has a collider and rigidbody.
  • The ground should also have a collider.
  • The ground also needs a script that can “listen” for collisions on its collider.
  • You need some way to identify if the thing that hit the ground is the player.

This code is an example of how this might be done.

EDIT: I haven’t watched it, but Unity has a video tutorial here.
https://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter

And this documentation should be useful:

// Ground.cs: Kills players that touch this.collider.
using UnityEngine;

// Attach this to the ground collider
public class Ground : MonoBehaviour
{
  // Called when another collider hits this one and one has a rigidbody.
  void OnCollisionEnter(Collision c)
  {
    // I just picked the name PlayerScript as an example.
    // Is the thing we collided with the Player?
    // I perform this check by seeing if the other collider has a PlayerScript attached.
    PlayerScript player = c.collider.GetComponent<PlayerScript>();
    if (player != null)
      player.Die(); // Does whatever is necessary to kill the player.
  }
}

So i’m still getting confused. Because i have no knowledge in coding :slight_smile:

I have added a rigidbody and box collider to my player object named Player, in the hierarchy and it is a car prefab model.
I have added a box collider to my floor, which is called grass and it is a plane
I’ve done everything to make this drive and fall off the tower, and when i hit the ground called grass i can still continue driving as it doesn’t kill me.

What names in this code do i need to change and what objects does the script need attaching to?

Sorry! I am a true beginner here keen to learn :slight_smile:

(Also i had a look at the video and document and i am still getting confused)

Ah yes, the previous technique works if you have a “PlayerScript” (made up name) script attached to the player.

If not, another popular way of identifying the player is by using tags.

Set the tag of the player’s collider to “Player”. You can do this in the Inspector window. Then you should be able to attach this new script to the grass collider.

It should show the collisions are working. It will probably make the car disappear completely.

// Ground.cs: Kills players that touch this.collider.
using UnityEngine;

// Attach this to the grass collider
public class Ground : MonoBehaviour
{

  // Called when another collider hits the grass.
  // This is part of Unity!
  void OnCollisionEnter(Collision c)
  {
    // Does the other collider have the tag "Player"?
    if (c.gameObject.tag == "Player")
    {
      // Yes it does. Destroy the entire gameObject.
      Destroy(c.gameObject);
    }
  }

}
1 Like

Ok cool thanks so much for providing the code! I believe it seems to be working, When i fell off it just went to a grey screen which said “scene is missing full screen camera”

Is there a way now, to make this bring up an overlay such as Gameover - Restart? Kind of thing, or for it to return me to the main menu of the game?

Thanks so much again :slight_smile:

What happened there is you have the camera attached as a child of the player, following the player around. When [Destroy](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Destroy)(c.gameObject); ran, it destroyed the player and the attached camera together.

GUIs can be pretty complicated things. So we should split this into two smaller tasks.

  • You should try setting up your menu in a separate scene and just see if you can turn it on and off.
  • After that, connect the menu to the car crash.

There is quite a bit of documentation. UICanvas and UIText should get you started. (A part of becoming a programmer is reading through documentation. The more you read, and the more you experiment, the more the documentation makes sense.)

If you have questions about the UI, you should make a new thread on the forum about that UI question specifically!

1 Like

I already have a function main menu, So when launching the game, it takes me to the main menu then when i select singleplayer it puts me into this scene that i’m working on.

I have added a second camera, pointing down at the map, so when the player dies it pans to this camera now and it is looking down at the map.

I want then for the game over text to then be enabled.

All i have, looking at my other code is

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameOver : MonoBehaviour
{

public Canvas Canvas;
public Button startText;
public Button exitText;

// Use this for initialization
void Start()
{

Canvas = Canvas.GetComponent();
startText = startText.GetComponent();
exitText = exitText.GetComponent();
Canvas.enabled = false;

}

But when i launch the game, the overlay is appearing and won’t hide and i don’t understand how i will be able to make the canvas appear when the player dies :slight_smile:
(ok updated, i made the canvas hidden when the game first launches, i forgot to add this script to the canvas! :))

Please use the CODE tags!

If GameOver already exists in the scene but is hidden, here is a quick example that might work.

// Ground.cs: Kills players that touch this.collider.
using UnityEngine;

// Attach this to the grass collider
public class Ground : MonoBehaviour
{

  // Called when another collider hits the grass.
  // This is part of Unity!
  void OnCollisionEnter(Collision c)
  {
    // Does the other collider have the tag "Player"?
    if (c.gameObject.tag == "Player")
    {
      // Yes it does. Destroy the entire gameObject.
      Destroy(c.gameObject);

      // Find GameOver
      GameOver gameOver = FindObjectOfType<GameOver>();
      if (gameOver == null)
        Debug.LogWarning("Could not find GameOver!");
      else
        gameOver.Canvas.enabled = true; // Show it

    }
  }

}
1 Like

Sorry i’m really new to this!
So basically this is the code i’m using to hide my gameover menu, this is attached to the player, then the player was dragged onto the Canvas title Canvas. The two buttons in this canvas had the button component added and then the player was dragged into that to get the buttons functioning later on.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameOver : MonoBehaviour
{

public Canvas Canvas;
public Button startText;
public Button exitText;




// Use this for initialization
void Start()
{

Canvas = Canvas.GetComponent<Canvas>();
startText = startText.GetComponent<Button>();
exitText = exitText.GetComponent<Button>();
Canvas.enabled = false;

}

So the canvas that i’m trying to display is just titled, Canvas

I entered that code and when i die and the camera changes to camera 2, the canvas isn’t displaying. Do i need to add any tags to my canvas or change any of the names in the code to canvas instead of gameover?

(EDITED Ok i got it working, never mind! Thanks so much for the help!

So what kind of code would i need for the yes and no buttons?

Do you think something like this?

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameOver : MonoBehaviour
{

    public Canvas Canvas;
    public Button startText;
    public Button exitText;
   



    // Use this for initialization
    void Start()
    {

        Canvas = Canvas.GetComponent<Canvas>();
        startText = startText.GetComponent<Button>();
        exitText = exitText.GetComponent<Button>();
        Canvas.enabled = false;

   
    }

    public void StartLevel()
    {
        Application.LoadLevel(3);

    }
    public void ExitGame()

    {

        Application.LoadLevel(0);
    }
}

Ok i just tested that, and it works. Can’t believe i solved that one by myself xD

So now, when i select yes and it restarts the scene, the camera is still in camera 2. Do you know how to get this working back to normal? I will have a fiddle around in the mean time and keep you updated :slight_smile:

1 Like

So for now, i have changed the second camera to display 2 so when i die now, it finds the game over canvas and just has a plain background to it, no secondary camera angle of the map and no “can’t find full screen camera” error :slight_smile:

Thanks for the help! Be sure to follow me, as i have more questions for other topics, for features for my game :slight_smile:
Thank you again! You are great and very patient to put up with me :slight_smile:

1 Like

Just one last question actually, as i actually tested it without my pause menu (pressing esc bring up a menu) i am encountering some problems now.

When i hit esc the pause menu works fine, the mouse is enable and i can move and select the options with my mouse curser, when coming along to dying the game over canvas appears fine but the mouse doesn’t enable and i can not select yes or no now.

This is my pause menu script, and its javascript

//*******************************************************************************
//*                                                                                            *
//*                            Written by Grady Featherstone                                *
//                                        © Copyright 2011                                        *
//*******************************************************************************
var mainMenuSceneName : String;
var pauseMenuFont : Font;
private var pauseEnabled = false;           

function Start(){
    pauseEnabled = false;
    Time.timeScale = 1;
    AudioListener.volume = 1;
    Cursor.visible = false;
}

function Update(){

    //check if pause button (escape key) is pressed
    {
   
        //check if game is already paused       
        if(pauseEnabled == true){
            //unpause the game
            pauseEnabled = false;
            Time.timeScale = 1;
            AudioListener.volume = 1;
            Cursor.visible = false;           
        }
       
        //else if game isn't paused, then pause it
        else if(pauseEnabled == false){
            pauseEnabled = true;
            AudioListener.volume = 0;
            Time.timeScale = 0;
            Cursor.visible = true;
        }
    }
}

private var showGraphicsDropDown = false;

function OnGUI(){

GUI.skin.box.font = pauseMenuFont;
GUI.skin.button.font = pauseMenuFont;

    if(pauseEnabled == true){
       
        //Make a background box
        GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
       
        //Make Main Menu button
        if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Yes")){
            Application.LoadLevel(mainMenuSceneName);
        }
       
        //Make Change Graphics Quality button
            if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "No")){
           
            if(showGraphicsDropDown == false){
                showGraphicsDropDown = true;
            }
            else{
                showGraphicsDropDown = false;
            }
        }
       
        //Create the Graphics settings buttons, these won't show automatically, they will be called when
        //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
        //on it again....
        if(showGraphicsDropDown == true){
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
                QualitySettings.currentLevel = QualityLevel.Fastest;
            }
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
                QualitySettings.currentLevel = QualityLevel.Fast;
            }
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
                QualitySettings.currentLevel = QualityLevel.Simple;
            }
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
                QualitySettings.currentLevel = QualityLevel.Good;
            }
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
                QualitySettings.currentLevel = QualityLevel.Beautiful;
            }
            if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
                QualitySettings.currentLevel = QualityLevel.Fantastic;
            }
           
            if(Input.GetKeyDown("escape")){
                showGraphicsDropDown = false;
            }
        }
       
        //Make quit game button
        if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
            Application.Quit();
        }
    }
}

I’m not the best at UnityScript, but I’m guessing you need Cursor.visible=true; to run sometime when dying. This script does it when Esc is pressed (maybe? looks like an if statement is missing?). Dying is not the same as pressing Esc so it is not pausing your game.

You might be able to just put Cursor.visible = true; in that Ground.cs script. It is a little messy because really you should have one script that controls if the cursor is visible instead of multiple scripts.

Hi so i am making a game and i tried your script it just showed a grey screen and my character was still movable hence when i moved my cursor and pushed my buttons it was still moving please help me solve this

Please start your own thread. Reference the code here if need be, or post it afresh.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Please use code tags: https://discussions.unity.com/t/481379

I didn’t understand how to make a thread can u help me?