Unity 2d Platforming Enemy death in C# ?

Hey,

I’m very new to unity and I’ve been doing some basics (movement, Trigers for automatic doors) Been watching a bunch of tutorials.

I have made spikes with a Trigger, yet if I use it on an enemy he falls through the map, so I need a collider but don’t know how or what to rewrite how.

Best would be a simple script that I can add to an object that has my Enemy AI which has the collider that kills me and makes me respawn at the start. Preferably with some //notes so I see what you did.

Thanks a lot in advance, appreciate it a lot :slight_smile:

My Trigger for Death:

I also have the levelManager and Checkpoints in separate ones

using UnityEngine;
using System.Collections;

public class KillPlayer : MonoBehaviour {

    public LevelManager levelManager;

    // Use this for initialization
    void Start () {
        levelManager = FindObjectOfType<LevelManager>();
    }
   
    // Update is called once per frame
    void Update () {
   
    }
    void OnTriggerEnter2D(Collider2D other)

    {
        if(other.name == "Player")
        {
            //levelManager.RespawnPlayer();
            Application.LoadLevel ("Class Movement Script stuff");
        }
    }

}

My enemy AI:

//variable declarations
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;


// main update function
function Update ()
{
    Distance = Vector3.Distance(Target.position, transform.position);
   
    if (Distance < lookAtDistance)
    {
        GetComponent.<Renderer>().material.color = Color.yellow;
        lookAt();
    }
   
    if (Distance > lookAtDistance)
    {
        GetComponent.<Renderer>().material.color = Color.green;
    }
   
    if (Distance < attackRange)
    {
        GetComponent.<Renderer>().material.color = Color.red;
        attack ();
    }
}

function lookAt ()
{
    var rotation = Quaternion.LookRotation(Target.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}

function attack ()
{
    transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

Bump,

Please help :confused:

So your problem is that you have spikes with a trigger but enemies fall through it? Exactly you need to replace it with a collider.

I assume you already have a collider, but IsTrigger is set to true. Set it to false and replace your following code

void OnTriggerEnter2D(Collider2D other)
{
        if(other.name == "Player")
        {
            //levelManager.RespawnPlayer();
            Application.LoadLevel ("Class Movement Script stuff");
        }
}

with

void OnCollisionEnter2D(Collider2D other){
        if(other.name == "Player"){
                   //levelManager.RespawnPlayer();
                   Application.LoadLevel("Class Movement Script stuff");
        }
}

And that should do it, however there may be some errors because I wrote this off the top of my head, hopefully it’ll atleast set you in the right direction. :slight_smile:

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class Enemy : MonoBehaviour
{
    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
        {
            SceneManager.LoadScene("Your Scene name here");
        }
    }
}

So this worked for me