You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RatAttack2D/Assets/Scripts/Enemy_Frog.cs

85 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Frog : MonoBehaviour
{
public bool canMove;
Transform PL;
Rigidbody2D RB;
Animator Ani;
public LayerMask G;
public GameObject Tung;
GameObject Tunge;
LineRenderer LR;
private void Start()
{
RB = transform.GetComponent<Rigidbody2D>();
Ani = transform.GetComponent<Animator>();
LR = transform.GetComponent<LineRenderer>();
canMove = true;
}
private void Update()
{
if(Tunge != null)
{
LR.positionCount = 2;
LR.SetPosition(0, transform.position);
LR.SetPosition(1, Tunge.transform.position);
}
else
{
LR.positionCount = 0;
}
if (!canMove)
{
return;
}
if (PL == null)
{
FindPlayer();
}
else
{
GotPlayer();
}
}
private void FindPlayer()
{
if(Physics2D.OverlapCircle(transform.position, 50f, G))
{
PL = Physics2D.OverlapCircle(transform.position, 50f, G).transform;
}
}
private void GotPlayer()
{
if (!Physics2D.OverlapCircle(transform.position, 50f, G))
{
PL = null;
return;
}
if (Vector2.Distance(transform.position, PL.position) >= 20)
{
JumpToPlayer();
return;
}
Attack();
}
private void Attack()
{
Ani.Play("Frog_Shoot");
canMove = false;
}
private void JumpToPlayer()
{
RB.AddForce(new Vector2((PL.position - transform.position).normalized.x * 10, 10));
Ani.Play("Frog_Shoot");
canMove = false;
}
public void shoot()
{
Tunge = GameObject.Instantiate(Tung, transform.position, Quaternion.AngleAxis((Mathf.Atan2((transform.position.y - PL.position.y), (transform.position.x - PL.position.x)) * Mathf.Rad2Deg) + 180, Vector3.forward));
Tunge.GetComponent<Bullet>().setDmg = 5;
Tunge.GetComponent<Bullet>().OwnTag = transform.tag;
}
}