-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemyScript.cs
More file actions
100 lines (91 loc) · 3.19 KB
/
EnemyScript.cs
File metadata and controls
100 lines (91 loc) · 3.19 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
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
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;
public class EnemyScript : MonoBehaviour
{
private UnityEngine.Object firedBullet;
private GameObject player1;
public float moveSpeed = 2;
public float rotationSpeed = 0.9f;
public float fireCooldown = 0.5f;
private float fireCooldownTimer = 0;
private float deadZoneX = 9.4f;
private float deadZoneY = 9.4f;
private FiredBulletScript firedBulletScript;
// Start is called before the first frame update
void Start()
{
firedBullet = Resources.Load("FiredBullet");
player1 = GameObject.FindGameObjectWithTag("Player1");
fireCooldownTimer = fireCooldown;
}
// Update is called once per frame
void Update()
{
rotateTowardsPlayer();
moveForward();
checkCooldowns();
checkInScene();
}
void rotateTowardsPlayer()
{
Vector3 from = transform.up;
Vector3 to = player1.transform.position - transform.position;
float angle = Vector3.SignedAngle(from, to, transform.forward) * rotationSpeed * Time.deltaTime;
transform.Rotate(0, 0, angle);
Debug.DrawRay(transform.position, transform.up * 10, Color.green, 10);
}
void moveForward()
{
transform.position += transform.up * moveSpeed * Time.deltaTime;
}
void checkCooldowns()
{
if (fireCooldownTimer < fireCooldown)
{
fireCooldownTimer += Time.deltaTime;
}
}
void checkInScene()
{
if (transform.position.x > deadZoneX)
{
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
if (transform.position.x < -1 * deadZoneX)
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
if (transform.position.y > deadZoneY)
{
transform.position += Vector3.down * moveSpeed * Time.deltaTime;
}
if (transform.position.y < -1 * deadZoneY)
{
transform.position += Vector3.up * moveSpeed * Time.deltaTime;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "FiredBullet")
{
Destroy(gameObject);
}
}
private void OnTriggerStay2D(Collider2D trigger)
{
if (trigger.tag == "Player1" && fireCooldownTimer >= fireCooldown)
{
float radiusOffset = 0.5f;
float angle = transform.rotation.eulerAngles.z;
float x = radiusOffset * Mathf.Sin((Mathf.PI / 180) * (angle + 180));
float y = radiusOffset * Mathf.Cos((Mathf.PI / 180) * angle);
UnityEngine.Object bulletInstance = Instantiate(firedBullet, new Vector3(transform.position.x + x, transform.position.y + y, transform.position.z), transform.rotation);
firedBulletScript = bulletInstance.GetComponent<FiredBulletScript>();
firedBulletScript.playerMoveSpeed = moveSpeed;
fireCooldownTimer = 0;
}
}
}