-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
63 lines (52 loc) · 1.64 KB
/
Enemy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
// Movement Speed
public float speed = 0.05f;
// Current movement Direction
Vector2 dir = Vector2.right;
// Upwards push force
public float upForce = 800;
void FixedUpdate()
{
// Set the Velocity
GetComponent<Rigidbody2D>().velocity = dir * speed;
}
void OnTriggerEnter2D(Collider2D coll)
{
// Hit a destination? Then move into other direction
transform.localScale = new Vector2(-1 * transform.localScale.x,
transform.localScale.y);
// And mirror it
dir = new Vector2(-1 * dir.x, dir.y);
}
void OnCollisionEnter2D(Collision2D coll)
{
// Collided with BabyMario?
if (coll.gameObject.name == "BabyMario")
{
// Is the collision above?
if (coll.contacts[0].point.y > transform.position.y)
{
// Play Animation
GetComponent<Animator>().SetTrigger("Died");
// Disable collider so it falls downwards
GetComponent<Collider2D>().enabled = false;
// Push BabyMario upwards
coll.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * upForce);
// Die in a few seconds
Invoke("Die", 5);
}
else
{
// Kill BabyMario
Destroy(coll.gameObject);
}
}
}
void Die()
{
Destroy(gameObject);
}
}