Untitled

mail@pastecode.io avatar
unknown
csharp
10 days ago
3.3 kB
2
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyBehaviour : MonoBehaviour
{
   [SerializeField] private Transform target;


    [Header("Enemy stats")]
    [SerializeField] private float speed = 3f;
    [SerializeField] private float rotateSpeed = 0.0025f;
    [SerializeField] private int DamageDealt;

    [Header("Enemy Hit Stats")]
    [SerializeField] private GameObject HitSprite;
    public float timeToChangeSprite;
    private bool isEnemyHit;

    [Header("References")]
    [SerializeField]  private PlayerHealth Playerhealth;
    [SerializeField] private EnemyHealth Enemyhealth;
    private Rigidbody2D rb;

    private void Start()
    {
        GetReferences();
        HitSprite.SetActive(false);
    }

    private void Update()
    {
        // Get The Target
        if (!target)
        {
            GetTarget();
        }
        else
        {
            RotateTowardsTarget();
        }

        //Rotate the Target towards the player
    }

    private void FixedUpdate()
    {
        rb.velocity = transform.up * speed;
    }

    private void RotateTowardsTarget()
    {
        Vector2 targetDirection = target.position - transform.position;
        float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg - 90f;
        Quaternion q = Quaternion.Euler(new Vector3(0, 0, angle));
        transform.localRotation = Quaternion.Slerp(transform.localRotation, q, rotateSpeed);
    }


    private void GetTarget()
    {
        if (GameObject.FindGameObjectWithTag("Player"))
        {
            target = GameObject.FindGameObjectWithTag("Player").transform;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.CompareTag("Player"))
        {
            if(Playerhealth.health > 0)
            {
                Playerhealth.TakeDamage(DamageDealt);
            }
            if (Playerhealth.health <= 0)
            {
                Destroy(collision.gameObject);
                target = null;
            }
            
        }

        else if (collision.gameObject.CompareTag("Bullet"))
        {
            if(Enemyhealth.health > 0)
            {
                Enemyhealth.TakeDamage(10);
            }

            if (Enemyhealth.health <= 0)
            {
                Destroy(gameObject);
            }

            if(!isEnemyHit)
            {
                isEnemyHit = true;
                StartCoroutine("ChangeSpriteWhenHit");
            }

          
            Destroy(collision.gameObject);
        }
    }

    IEnumerator ChangeSpriteWhenHit()
    {
        HitSprite.SetActive(true);
        yield return new WaitForSeconds(timeToChangeSprite);
        HitSprite.SetActive(false);
        isEnemyHit = false;
    }

    private void GetReferences()
    {
        if (GameObject.FindGameObjectWithTag("Player"))
        {
            Playerhealth = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
        }

        Enemyhealth = GetComponent<EnemyHealth>();
        rb = GetComponent<Rigidbody2D>();
    }
}
Leave a Comment