-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveableMonster.cs
More file actions
49 lines (37 loc) · 1.27 KB
/
MoveableMonster.cs
File metadata and controls
49 lines (37 loc) · 1.27 KB
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
using UnityEngine;
using System.Collections;
using System.Linq;
public class MoveableMonster : Monster
{
[SerializeField]
private float speed = 2.0F;
private Vector3 direction;
private SpriteRenderer sprite;
protected override void Awake()
{
sprite = GetComponentInChildren<SpriteRenderer>();
}
protected override void Start()
{
direction = transform.right;
}
protected override void Update()
{
Move();
}
protected override void OnTriggerEnter2D(Collider2D collider)
{
Unit unit = collider.GetComponent<Unit>();
if (unit && unit is Character)
{
if (Mathf.Abs(unit.transform.position.x - transform.position.x) < 0.3F) ReceiveDamage();
else unit.ReceiveDamage();
}
}
private void Move()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position + transform.up * 0.5F + transform.right * direction.x * 0.5F, 0.1F);
if (colliders.Length > 0 && colliders.All(x => !x.GetComponent<Character>())) direction *= -1.0F;
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
}
}