How to make a killzone ?

Hi,

I don’t see how can I do :
When the player hit the BoxCollider2D of the GameObject called “Killzone”
-Reset the position of the player to 0, I mean reset the Scene by when we start the game or by a checkpoint
I have a few idea but no any idea to make the relation between the player and the BoxCollider2D

Thanks and please, don’t give me the code directly or explain me why you do it ?

Make sure you have a BoxCollider2D component attached to you Killzone Object and any Collision2D component attached to your player. Make a script for your Killzone object and add a method called OnCollisionEnter2D. This method will be called when an object is colliding with the Killzone object.
example:

public class Killzone: MonoBehaviour {
    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "Player"){

            //Execute this code if the colliding object is the player, to access the player gameObject, use coll.
       }
    }
}

The parameter “coll” in the code is the gameObject that collides with the Killzone.
First you check if the gameObject the Killzone is colliding with is the player (Note: you have to set the tag “Player” to your player object for the code above to make sense).
If the colliding object is the player, then execute your code.

Is it possible to make a 3D killzone? I know this is the 2D section but i can’t find an answer anywhere. Thanks :slight_smile:

Do the same thing, except with a 3D collider instead of a 2D and OnCollisionEnter instead of OnCollisionEnter2D.

(Although I would use OnTriggerEnter instead of OnCollisionEnter in either case)

1 Like

So you mean like this:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
  

public class Killzone: MonoBehaviour {

    void OnTriggerEnter(BoxCollider coll) {
        if (coll.gameObject.tag == "Cube"){
            //Execute this code if the colliding object is the player, to access the player gameObject, use coll.
       }
    }
}

Sorry i have never written code myself:sweat_smile:

that’s pretty close.

for void OnTriggerEnter(BoxCollider coll)
Did that actually work? I would think that for OnTriggerEnter to be triggered that the method signature would have to match how it is in the documentation. In other words the parameter probably needs to be Collider instead of a BoxCollider
void OnTriggerEnter(Collider coll)
Collider will include any type of 3D collider. In some cases, you want to detect more than one type of object that enters the trigger area, and these may different types of colliders.

This code is meant to be attached to the killzone, so remember that the Collider being passed to the function is not the collider of the killzone. Rather, It’s going to be the collider of what ever object enters the killzone. In this case, the intent is specifically to target the player, so in this case
if (coll.gameObject.tag == "Cube"){
Should probably be something more like:
if (coll.gameObject.tag == "Player"){ or whatever you have the player tagged as.

Here we go guys:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KillZone : MonoBehaviour
{
// functon for when the object enters the kill volume by handle with it’s collider
void OnTriggerEnter(Collider Col)
{
// checks to see if the object that entered the trigger is the Player
if (!Col.CompareTag (“Player”)) return;
// takes the collider from the game object(Col.gameObject) and stores in PlayerObject
GameObject PlayerObject = Col.gameObject;
//accessing the Healthscript
Health PlayerHealth = PlayerObject.GetComponent ();
// to reduce the health points inside Health, take their actual value minus damage value (-10 points here)
PlayerHealth.HealthPoints = PlayerHealth.HealthPoints - 10f;
}
}