MY ENEMY SCRIPT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
// Public Variables
public float speed;
public GameObject effect;
// Private Variables
private Transform playerPos;
private Player player;
private Shake shake;
// Functions
private void Start()
{
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent();
player = GameObject.FindGameObjectWithTag("Player").GetComponent();
playerPos = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
shake.CamShake();
Instantiate(effect, transform.position, Quaternion.identity);
player.health--;
Destroy(gameObject);
}
if (other.CompareTag("Projectile"))
{
shake.CamShake();
Instantiate(effect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
↧