- Home /
I want my player to die when collide with enemy from front or back and not when it jumps on it (i alread tried checking if player is grounded or not) and if someone could provide script for it ill be glad
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Enemy : MonoBehaviour {
public float velocity = 1f;
public Transform sightStart;
public Transform sightEnd;
public bool colliding;
private Rigidbody2D rigi;
public LayerMask Detect;
public Transform weakness;
Animator anime;
void Start ()
{
anime = GetComponent<Animator> ();
rigi = GetComponent<Rigidbody2D> ();
}
void Update ()
{
rigi.velocity = new Vector2 (velocity, rigi.velocity.y);
colliding = Physics2D.Linecast (sightStart.position, sightEnd.position,Detect);
if (colliding && gameObject.tag != "Player") {
transform.localScale = new Vector2 (transform.localScale.x * -1, transform.localScale.y);
velocity *= -1;
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawLine (sightStart.position, sightEnd.position);
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag == "Player")
{
float height = col.contacts [0].point.y - weakness.position.y;
if (height >0)
{
Dies ();
}
}
}
void Dies ()
{
//anim.SetBool ("stomped", true);
Destroy (this.gameObject, 0.5f);
//gameObject.tag = "phew";
}
}
Comment
You could check if the player position is higher than the collider's position + it's bounds extents or you could use 2 separate colliders.
Your answer
Follow this Question
Related Questions
Scripting a Death Fall 1 Answer
How to prevent losing multiple lives at zero health? 2 Answers
Camera rotation around player while following. 6 Answers
OnCollisionEnter issues 3 Answers
Help with player death on collision! 2 Answers