-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeBlayer.cs
118 lines (89 loc) · 3.08 KB
/
seeBlayer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using Unity.VisualScripting;
using UnityEngine;
public class seeBlayer : MonoBehaviour
{
Transform player;
// A* variabler
public Transform[] patrolePosition;
private Transform[] CurrentPosition;
public int delay = 0;
int index = 0;
IAstarAI agent;
float switchTime = float.PositiveInfinity;
// aggro variabler
float firstAggroTime = -1f;
public float aggroTime = 0.5f;
AIDestinationSetter AD;
// shooting at player:
bool playerAggro = false;
[SerializeField]
enemyWeaponHolder weaponHolder;
void Start()
{
// find spiller objekt og relevante komponenter
player = GameObject.Find("Blayer").transform;
AD = GetComponent<AIDestinationSetter>();
agent = GetComponent<IAstarAI>();
CurrentPosition = patrolePosition;
}
void Update(){
// tjek om spiller er indenfor synsvidde
if((player.transform.position - gameObject.transform.position).magnitude < 20f)
{
// lav raycast fra fjende til spilleren
Vector2 mouseDirection = player.position - gameObject.transform.position;
Ray2D ray = new Ray2D(gameObject.transform.position, mouseDirection.normalized);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 20f, (1 << 7) | (1 << 9));
// tjek om spilleren er første objekt i raycast
if(hit.collider != null && hit.collider.CompareTag("Player"))
{
// spilleren er set
// håndter første aggro tid
if (playerAggro == false){
firstAggroTime = Time.time;
}
playerAggro = true;
AD.target = player;
CurrentPosition = null;
}
else
{
// spiller er ikke set
playerAggro = false;
AD.target = null;
CurrentPosition = patrolePosition;
Patrole();
}
}
else
// spiller er ude af synsvidde
{
Patrole();
}
// hvis spilleren er set og første aggro tid er gået, skyd
if(playerAggro && firstAggroTime + aggroTime < Time.time){
weaponHolder.onShoot();
}
}
// patruljer mellem positioner
void Patrole(){
// håndter om der er gemte patrulje punkter
if(CurrentPosition == null || CurrentPosition.Length == 0) return;
// skift til næste patrulje punkt
bool search = false;
if (agent.reachedEndOfPath && !agent.pathPending && float.IsPositiveInfinity(switchTime)){
switchTime = Time.time + delay;
}
if (Time.time >= switchTime){
index ++;
search = true;
switchTime = float.PositiveInfinity;
}
index %= CurrentPosition.Length;
agent.destination = CurrentPosition[index].position;
if (search) agent.SearchPath();
}
}