Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 2d Platforming Enemy death in C# ?

Discussion in 'Scripting' started by Trickzbunny, Jun 26, 2015.

  1. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    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 :)



    My Trigger for Death:

    I also have the levelManager and Checkpoints in separate ones

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class KillPlayer : MonoBehaviour {
    5.  
    6.     public LevelManager levelManager;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.         levelManager = FindObjectOfType<LevelManager>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.     void OnTriggerEnter2D(Collider2D other)
    18.  
    19.     {
    20.         if(other.name == "Player")
    21.         {
    22.             //levelManager.RespawnPlayer();
    23.             Application.LoadLevel ("Class Movement Script stuff");
    24.         }
    25.     }
    26.  
    27. }
    28.  

    My enemy AI:

    Code (CSharp):
    1. //variable declarations
    2. var Distance;
    3. var Target : Transform;
    4. var lookAtDistance = 25.0;
    5. var attackRange = 15.0;
    6. var moveSpeed = 5.0;
    7. var Damping = 6.0;
    8.  
    9.  
    10. // main update function
    11. function Update ()
    12. {
    13.     Distance = Vector3.Distance(Target.position, transform.position);
    14.    
    15.     if (Distance < lookAtDistance)
    16.     {
    17.         GetComponent.<Renderer>().material.color = Color.yellow;
    18.         lookAt();
    19.     }
    20.    
    21.     if (Distance > lookAtDistance)
    22.     {
    23.         GetComponent.<Renderer>().material.color = Color.green;
    24.     }
    25.    
    26.     if (Distance < attackRange)
    27.     {
    28.         GetComponent.<Renderer>().material.color = Color.red;
    29.         attack ();
    30.     }
    31. }
    32.  
    33. function lookAt ()
    34. {
    35.     var rotation = Quaternion.LookRotation(Target.position - transform.position);
    36.     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
    37. }
    38.  
    39. function attack ()
    40. {
    41.     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    42. }
    43.  
     
  2. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Bump,

    Please help :/
     
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    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

    Code (CSharp):
    1.  
    2. void OnTriggerEnter2D(Collider2D other)
    3. {
    4.         if(other.name == "Player")
    5.         {
    6.             //levelManager.RespawnPlayer();
    7.             Application.LoadLevel ("Class Movement Script stuff");
    8.         }
    9. }
    10.  
    with

    Code (CSharp):
    1.  
    2. void OnCollisionEnter2D(Collider2D other){
    3.         if(other.name == "Player"){
    4.                    //levelManager.RespawnPlayer();
    5.                    Application.LoadLevel("Class Movement Script stuff");
    6.         }
    7. }
    8.  
    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. :)
     
  4. diegogutimail

    diegogutimail

    Joined:
    Jul 24, 2020
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     void OnCollisionEnter2D(Collision2D coll)
    8.     {
    9.         if (coll.gameObject.tag == "Player")
    10.         {
    11.             SceneManager.LoadScene("Your Scene name here");
    12.         }
    13.     }
    14. }
    So this worked for me