-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyControl.cs
More file actions
47 lines (45 loc) · 1.15 KB
/
EnemyControl.cs
File metadata and controls
47 lines (45 loc) · 1.15 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour
{
private Rigidbody rBody;
private Animation ani;
private Transform player;
// Start is called before the first frame update
void Start()
{
rBody = GetComponent<Rigidbody>();
ani = GetComponent<Animation>();
player = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void Update()
{
if(player==null)
{
return;
}
float dis = Vector3.Distance(transform.position, player.position);
if(dis<2f)
{
//杀死玩家
Destroy(player.gameObject);
//站立
ani.Play("idle");
}
else if(dis<5)
{
//朝向玩家
transform.LookAt(player.position);
//追击玩家
rBody.velocity = transform.forward * 2;
ani.Play("move");
}
else
{
//待机
ani.Play("idle");
}
}
}