Unity中敌人简单逻辑的实现(来回走动,攻击)2D

unity自带一套自动巡航系统,但是目前应该先了解最基本的使用代码控制敌人实现逻辑(1来回走动,2发现玩家时追着玩家,3进入敌人攻击范围时进行攻击),一般来说这是最基本的敌人的功能

分析完敌人所具备的能力后,就将敌人的能力进行拆解,分别进行实现

一 来回走动:敌人在自己的视力范围内没有见到玩家,就会自动来回走动巡逻

敌人的来回移动可以改变transform.position属性来实现,需要设置两个参数,一个敌人移动的速度,一个敌人移动多少秒后转向,也就是时间跨度

在实现完来回走动后会出现敌人会出现一个问题,那就是敌人只会来回移动,不会转向,当敌人改变移动方向时,用transform.localRotation属性进行转向设置,代码如下

 public float Speed;//移动速度
 public int timeSpan;//单向移动时间,60帧为一秒(因为移动函数在fixupdate中)这是外部设置的时间夸度
 private int time;
 int Ry = 0;//怪物转向角度为180度
void Start()
{    
    time = timeSpan;
}
 private void FixedUpdate()
{
    Move();
}
void Move()
{
    Vector2 position = transform.position;
    timeSpan--;       
    if(timeSpan<=0)
    {
        Speed = -Speed;
        if(Speed>0)
        {
            Ry = 0;
        }
        else
        {
            Ry = -180;
        }
        gameObject.transform.localRotation = Quaternion.Euler(0,Ry, 0);//怪物巡逻转向
        timeSpan = time;
    }
    position.x += Speed;
    transform.position = position;       
}

二 跟随玩家:当敌人发现玩家的时候,敌人就会跟随玩家。或者交靠近玩家,目的是为了攻击

  1. 首先为敌人添加一个碰撞器(勾选触发器)用来模拟敌人的视线

  1. 当玩家进入敌人的视角触发器时,敌人就需要向玩家靠拢

bool isFind=false;//怪物是否发现玩家,如果发现玩家需要将怪物的状态由巡逻切换为跟随玩家
public GameObject player;//获取到怪物跟随的玩家
private void FixedUpdate()
{
    if (isFind == false)
    {
        Move();//这是巡逻函数
    }
    else if (isFind == true)
    {
        Follow();//这是跟随函数
    }
}
 void Follow()
    {
    Vector2 position=transform.position;
    if(Mathf.Abs(gameObject.transform.position.x - player.transform.position.x)>5)//怪外和角色距离大于某个值时才开始朝玩家移动,这样避免了怪物与玩家冲重合
    {
        if (gameObject.transform.position.x < player.transform.position.x)
        {
            position.x += Speed;
            gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0);//怪物转向
        }
        else if (gameObject.transform.position.x > player.transform.position.x)
        {
            position.x -= Speed;
            gameObject.transform.localRotation = Quaternion.Euler(0, -180, 0);//怪物转向
        }
        gameObject.transform.position = position;
    }        
}
private void OnTriggerEnter2D(Collider2D collision)//玩家进入了怪物的视角(触发器),怪物就会攻击玩家
{
    if (collision.gameObject.tag == "Player")//需要将玩家的标签设为Player
    {
        isFind = true;
    }        
}

三 攻击玩家:当玩家进入到了敌人的攻击范围后敌人就需要进行攻击(如何实现攻击在另一篇有讲)

Animator animator;
 void Start()
{
    animator = GetComponent<Animator>();//敌人的攻击动画
}
 private void FixedUpdate()//完整的fixupdate包含的函数
{
    if (isFind == false)
    {
        Move();
    }
    else if (isFind == true)
    {
        Follow();
        Attack();
    }
}
  void Attack()
{
    if (Mathf.Abs(gameObject.transform.position.x - player.transform.position.x) < 5)//达到可以攻击的距离
    {
        animator.SetBool("CanAttack", true);  
    }
    else 
    {
        animator.SetBool("CanAttack", false);
    }
    }

最后将这三个功能代码进行整合放在fixupdate中就能实现敌人的基础逻辑

  • 9
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单Unity 2D敌人攻击的代码示例: ```c# public class EnemyAttack : MonoBehaviour { public float damage = 10f; public float attackDelay = 1f; public float attackRange = 1f; public LayerMask attackMask; private Animator animator; private Transform target; private float nextAttackTime = 0f; void Start() { animator = GetComponent<Animator>(); target = GameObject.FindGameObjectWithTag("Player").transform; } void Update() { if (Time.time >= nextAttackTime) { float distanceToTarget = Vector2.Distance(transform.position, target.position); if (distanceToTarget <= attackRange) { Attack(); nextAttackTime = Time.time + attackDelay; } } } void Attack() { animator.SetTrigger("attack"); Collider2D[] hitTargets = Physics2D.OverlapCircleAll(transform.position, attackRange, attackMask); foreach (Collider2D target in hitTargets) { target.GetComponent<PlayerHealth>().TakeDamage(damage); } } void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, attackRange); } } ``` 该代码包含了以下功能: - 敌人使用 `attackDelay` 变量来限制攻击频率。 - 敌人使用 `attackRange` 变量来限制攻击范围。 - 敌人使用 `attackMask` 变量来指定攻击时需要检测的层。 - 敌人使用 `animator` 变量来播放攻击动画。 - 敌人使用 `OnDrawGizmosSelected()` 函数来在场景视图显示攻击范围的可视化。 - 敌人使用 `Physics2D.OverlapCircleAll()` 函数来检测攻击范围内的目标。 - 敌人使用 `TakeDamage()` 函数来对检测到的目标造成伤害。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值