-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cs
68 lines (62 loc) · 1.8 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
64
65
66
67
68
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
private Transform player;
private Vector2 targetPos;
public int soomthing = 6;
public float restTime = 1f;
public float restTimer = 0;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
targetPos = transform.position;
}
private void Update()
{
GetComponent<Rigidbody2D>().MovePosition(Vector2.Lerp(transform.position,targetPos,soomthing*Time.deltaTime));
restTimer += Time.deltaTime;
if (restTimer>restTime) {
Move();
}
}
public void Move()
{
Vector2 offset = player.position - transform.position;
if (offset.magnitude < 1.1f)
{
GetComponent<Animator>().SetTrigger("Attack");
}
else {
if (Mathf.Abs(offset.y) > Mathf.Abs(offset.x)){
if (offset.y < 0)
{
MoveTo(0, -1);
}
else {
MoveTo(0,1);
}
}
else
{
if (offset.x < 0)
{
MoveTo(-1, 0);
}
else {
MoveTo(1,0);
}
}
}
restTimer = 0;
}
private void MoveTo(float x,float y) {
GetComponent<BoxCollider2D>().enabled = false;
RaycastHit2D hit = Physics2D.Linecast(targetPos, targetPos + new Vector2(x, y));
GetComponent<BoxCollider2D>().enabled = true;
if (hit.transform == null)
{
targetPos += new Vector2(x, y);
}
}
}