Pracownia-Programowania-Pro.../Assets/Scripts/Enemy Scripts/EnemyBullet.cs
Polarjad 1ea616dd60 Added dungeon textures, added scenes, added UI
Enemy balance, added upgrades, added score, added menu
2021-01-25 22:40:51 +01:00

53 lines
993 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBullet : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
private Transform player;
private Vector2 target;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
target = new Vector2(player.position.x, player.position.y);
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
if(target.x == transform.position.x && target.y == transform.position.y)
{
DestroyBullet();
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
DestroyBullet();
Debug.Log("Player damage taken");
}
if (other.CompareTag("Obstacle"))
{
DestroyBullet();
Debug.Log("Obstacle detected");
}
}
void OnBecameInvisible()
{
Debug.Log("Invisible bullet");
DestroyBullet();
}
void DestroyBullet()
{
Destroy(gameObject);
}
}