Can't get my Land Mine to work Properly

So, I have two bits of code that I’m using, “PlaceMine” & “StepOnMine.”

My goal is to have my “Player” place a Mine that will only destroy enemies who step on it.

What it is doing is playing the explosion sound as soon as the player places it. then it is totally unresponsive.

Thanks in advance for and help provided.

“PlaceMine” attached to my player and appears to work correctly.

using UnityEngine;
using System.Collections.Generic;

public class PlaceMine : MonoBehaviour
{
    // Gets the gameObject
    public Object mine;
    // Gets the input key
    public string placeMine = "space";

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown (placeMine)) {
            Instantiate (mine, this.transform.position, this.transform.rotation);
        }
    }
}

“StepOnMine” is attached to my land Mine and is playing sound as soon as I place it. After that it appears to be totally unresponsive.

using UnityEngine;
using System.Collections;

public class StepOnMine : MonoBehaviour
{

    // check for collision and detect collider
    void OnCollision (Collider col)
    {

        //if a collider tag is not equal to gameObject with the tag "Player"
        if (col.gameObject.tag != "Player") {

            // get audio file
            AudioSource audio = GetComponent<AudioSource> ();

            // play audio file
            audio.Play ();

            // destroy collider
            Destroy (col.gameObject);

            // destroy itself
            Destroy (this.gameObject);
        }
    }
}

first thought is that it’s colliding with the ground… you only specify “not the player”

is anything destroyed or are there any errors being thrown (I’d expect one of the other of these tbh)

at the moment all i have in my scene is the Player, Zombies that chase the Player, and the land mines that are placed by the player as he is being chased, the ground is just a camera background color and not an actual object.

upon further testing I believe that my collision is simply never being triggered.

could it be because I have my mine on a separate sorting layer than my player and zombie?

I’d put the player, zombies, and mines all on separate layers and uncheck interactions between the mine and player layer. Then you can get rid of the tag check completely. Also, if you do this in your collision message

Debug.Log("collision", col.gameObject);
Debug.Break();

it will pause the editor and clicking on the “collision” message in the console will highlight the GameObject that triggered the message.

It’s also possible that your audio source is configured to play on awake which would play the sound immediately.

1 Like