I want to check if my player/cube walks over a certain area...

I’m working on a game with a friend and I want to check if the player is walking over a “DeathZone” /colliding with it by having the death zone’s trigger being set off, but only by the player so then the player can be destroyed and be respawned. How would I do that? I’m new to coding I understand the logic part of it, but not the terms. Anyway, any help is gladly appreciated.

@TheCreator34 If you want a death zone, then you’ll need to create a tag for it in order for Unity to differentiate it from other gameObjects.

Like so:

  • Make sure the DeathZone Collider isTrigger is ticked.
  • Add another collider to your death zone with isTrigger un-ticked (so the player can walk on it)
  • Make sure your player has a Rigidbody component attached to it.

Now you have a death zone and with a little bit of code, your player has the means to detect this death zone. Next, if you want to make a basic and simple death+re-spawn system, I would use a health and re-spawn system in conjunction. To do so I created a “PlayerHealth” script and a “DeathRespawn” script (C#).

For the player health, simply assign an integer value = 100 to represent how much health the player has. When this value drops to 0 or less, this script fetches the ReSpawn() function from the “DeathRespawn” script.

Like so:

public class PlayerHealth : MonoBehaviour {

    public DeathRespawn dr;

    private int health = 100;
	
	void Update () 
	{
        //if player health is less than or equal to 0, respawn player (script will use DeathRespawn script to do so) and reset health
        if (health <= 0)
        {
            dr.ReSpawn();
            health = 100;
        }
	}

    void OnTriggerStay(Collider other)
    {
        //if player is on a gameObject with the tag "DeathZone", substract 1 health per frame 
        if (other.gameObject.tag == "DeathZone")
        {
            health--;
        }
    }
}

The most basic re-spawn consists of moving the player back to where it started and resetting its health. Health was already reset above, after fetching and running ReSpawn(), therefore what’s left is to move the player back where it started.

Like so:

public class DeathRespawn : MonoBehaviour {

    public GameObject player;

    public void ReSpawn()
    {
        //move the player to 0,0,0 (x,y,z)
        player.transform.position = new Vector3(0,1,0);
    }
}

Attach “PlayerHealth” to the player and “DeathRespawn” to your death zone. Be sure to plug in the player to “DeathRespawn” and the death zone to “PlayerHealth” (“DR”) in the inspector.

If you have any questions, don’t hesitate to ask!

A side note: there are many great tutorials showing different and interesting methods on YouTube and the Unity Forums, go check them out!

Looks like you’ll be needing Trigger Colliders.

You can add a Collider component to your “Death Zone” and check the “is Trigger” box.
Then in your Player Script (The player should also have a collider) use the method OnTriggerEnter to detect if the Player and the Zone collide.

Here is a good tutorial on this topic: On Trigger Start / Stay / Exit [Tutorial][C#] - Unity 3d - YouTube

To Destroy a GameObject use Destroy(); - but if your player is supposed to respawn right away you could also just reset his position.

Good Luck!