-
Notifications
You must be signed in to change notification settings - Fork 0
/
enermy2.cs
88 lines (82 loc) · 2.77 KB
/
enermy2.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//This class contains the script for the enemy game object that can fire projectiles.
public class enermy2 : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform firePoint;
public Rigidbody2D rb;
public Rigidbody2D rb3;
private bool difficulty;
private float moveSpeed;
private float lookraduis;
private int interval;
private int bulletForce;
private GameObject player;
private int counter;
private Vector2 movement;
private float distance;
// Start is called before the first frame update to set the neccessary variable of the enemy object.
void Start()
{
moveSpeed = 1;
lookraduis = 6f;
difficulty = setvol.isHard;
player = GameObject.Find("player");
if(difficulty == true)
{
interval = 115;
bulletForce = 8;
}
else
{
interval = 130;
bulletForce = 6;
}
}
// Update is called once per frame to make the game object face the player.
void Update()
{
distance = Vector3.Distance(player.transform.position, transform.position);
if (distance <= lookraduis)
{
Vector3 direction = player.transform.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 80f;
rb.rotation = angle;
direction.Normalize();
movement = direction;
}
}
//This function is called in a fixed interval that move the game object towards the player and fire a projectile with a longer interval.
void FixedUpdate()
{
if (distance <= lookraduis)
{
if (counter == interval)
{
counter = 0;
shoot();
}
else
{
counter += 1;
}
}
moveChatacter(movement);
}
//This function moves the game object by a certain distance.
void moveChatacter(Vector2 direction)
{
rb3.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
rb.transform.position = rb3.transform.position;
}
//This function is called to create a bullet object and fire it towards the player.
void shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());
Rigidbody2D rb2 = bullet.GetComponent<Rigidbody2D>();
rb2.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}