Unity2D 敌人追踪/攻击/移动AI 第二期

AI功能简介:这个AI是在第一期的基础上进行修改后的AI,第一期的AI不能够自动追踪Palyer,只能够停留在原地不动,现在能够去自动追踪主角,只要进入了追踪范围内,就会一直追踪玩家,直至玩家离开追踪范围或被消灭,相关代码如下,操作与第一期相同,第一期链接如下:AI 第一期
展示:
在这里插入图片描述

脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bunny : MonoBehaviour
{
    public static Bunny bunny;
    private void Awake()
    {
        bunny = this;
    }

    Animator animator;
    Rigidbody2D rg;
    public float MoveSpeed = 0;
    float changeTime = 0;
    float time = 0;

    //随机时间
    public float minTime = 0;
    public float maxTime = 0;

    int direction = 1;
    float distance_ = 0;
    bool isChange = false;

    bool canHit = false;

    Transform PlayerTransform;
    public float Distance = 0;


    float Dwell_gap = 0; //停留时间
    float Dwell_Cumulative = 0; //停留时间累积
    public float DwellMinTime = 0;
    public float DwellMaxTime = 0;


    float moveTime = 0; //行走时间
    //行走的最大时间和最短时间设定
    public float moveMinTime = 0;
    public float moveMaxTime = 0;

    float moveTimeCumulative = 0;

    bool isDwell = false;
    bool isDwellRange = false;

    bool CanMove = false;
    bool CanCount = false;
    bool CanTimeCumulative = false;

    bool isAttacking = false;
    bool isAttacked = false;

    public bool TrackPlayer = false;
    public float TrackSpeed = 0;

    public float CurrentPlayer = 0;

    public float Life_value = 0;

    public float hitTime = 0;
    bool isHit = false;
    float time1 = 0;

    public float TrackDistance = 0;

    GameObject gm;

    bool isinvincible = false;

    public float destory_door_value = 0;

    void Start()
    {
        animator = GetComponent<Animator>();
        rg = GetComponent<Rigidbody2D>();
        CanMove = false;
        CanCount = true;
    }


    void Update()
    {
        PlayerTransform = GameObject.Find("Player").transform;
        //得到与玩家的距离
        distance_ = Mathf.Sqrt(Mathf.Abs(PlayerTransform.transform.position.x - transform.position.x) * Mathf.Abs(PlayerTransform.transform.position.x - transform.position.x) +
        Mathf.Abs(PlayerTransform.transform.position.y - transform.position.y) * Mathf.Abs(PlayerTransform.transform.position.y - transform.position.y));

        time = time + Time.deltaTime;

        if (isChange == false)
        {
            changeTime = Random.Range(minTime, maxTime);
            isChange = true;
        }

        transform.localScale = new Vector3(-direction, 1, 1);

        if (time > changeTime)
        {
            direction = -direction;
            transform.localScale = new Vector3(-direction, 1, 1);
            time = 0;
            isChange = false;
        }

        //判断是否在攻击距离之内
        if (distance_ < Distance)
        {
            canHit = true;
        }
        else
        {
            canHit = false;
        }


        if (canHit&&TrackPlayer)
        {
            //判断玩家在怪物的哪边
            if (PlayerTransform.transform.position.x < transform.position.x)
            {
                direction = 1;
                animator.SetBool("attack", true);
                GMF_PlayerController._PlayeController.inJured(CurrentPlayer);
                transform.localScale = new Vector3(direction, 1, 1);
            }
            else
            {
                direction = -1;
                animator.SetBool("attack", true);
                transform.localScale = new Vector3(direction, 1, 1);
                GMF_PlayerController._PlayeController.inJured(CurrentPlayer);
            }
        }
        else
        {
            animator.SetBool("attack", false);
        }

        //随机暂停时间 开始暂停 播放停留动画 人物停止移动 生成暂停时间
        if (CanCount)
        {
            isDwellRange = true;
            CanTimeCumulative = true;
            CanCount = false;
        }

        if (isDwellRange) //设置停留动画 设置暂停时间 
        {
            Dwell_gap = Random.Range(DwellMinTime, DwellMaxTime);
            animator.SetBool("idem", true);
            animator.SetBool("run", false);
            CanMove = false;
            isDwellRange = false;
        }

        if (CanTimeCumulative)
        {
            Dwell_Cumulative = Dwell_Cumulative + Time.deltaTime;
        }


        if (Dwell_Cumulative > Dwell_gap) //超过暂停时间 停留结束 结束停留动画 生成行走时间 开始行走动画
        {
            animator.SetBool("idem", false);
            animator.SetBool("run", true);
            moveTime = Random.Range(moveMinTime, moveMaxTime);
            CanMove = true;
            Dwell_Cumulative = 0;
            CanTimeCumulative = false;
        }

        //行走时间结束 
        if (CanMove)
        {

            moveTimeCumulative = moveTimeCumulative + Time.deltaTime;
            if (moveTimeCumulative > moveTime)
            {
                CanMove = false;
                moveTimeCumulative = 0;
                CanCount = true;
            }
        }

        if (Life_value <= 0)
        {
            Destroy(this.gameObject);
        }

        if (isHit)
        {
            // animator.SetBool("current", true);
            time1 = time1 + Time.deltaTime;
            if (time1 > hitTime)
            {
                animator.SetBool("current", false);
                isinvincible = false;
                isHit = false;
                time1 = 0;
            }
        }
    }

    private void FixedUpdate()
    {
        //没有停留就可以移动
        if (CanMove)
        {
            MoveMent();
        }

    }

    void MoveMent()
    {

        if (TrackPlayer)
        {
            if (distance_ < TrackDistance)
            {
                CanMove = true;
                Vector2 temp = Vector2.MoveTowards(transform.position, PlayerTransform.position, MoveSpeed * 2 * Time.deltaTime);
                //考虑到可能有碰撞检测,所以使用刚体的移动方式
                GetComponent<Rigidbody2D>().MovePosition(temp);

                //判断玩家在怪物的哪边
                if (PlayerTransform.transform.position.x < transform.position.x)
                {
                    direction = -1;
                    transform.localScale = new Vector3(direction, 1, 1);
                }
                else
                {
                    direction = 1;
                    transform.localScale = new Vector3(direction, 1, 1);
                }
            }
            else
            {
                rg.velocity = new Vector2(1 * direction * MoveSpeed, 0);
            }
        }
        else
        {

            rg.velocity = new Vector2(1 * direction * MoveSpeed, 0);

        }

    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            CanMove = true;
        }

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            CanMove = false;
        }

        if (collision.tag == "bullte" || collision.tag=="hammer")
        {
            if (isinvincible == false)
            {
                animator.SetBool("current", true);
                isinvincible = true;
                isHit = true;
            }
        }

    }

    public void Current(float value)
    {
        Life_value = Life_value - value;
    }

}

  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个敌人追踪玩家的功能,你可以考虑以下几个步骤: 1. 获取敌人和玩家的位置信息。可以通过敌人和玩家的 transform 组件来获取它们的位置信息。 2. 计算敌人到玩家的距离。可以使用 Vector3.Distance 方法来计算两个位置之间的距离。 3. 判断敌人能否看到玩家。可以使用 Physics2D.Linecast 方法来判断敌人和玩家之间是否有障碍物。如果没有障碍物,说明敌人能够看到玩家。 4. 如果敌人能够看到玩家,就让敌人向玩家移动。可以使用 Rigidbody2D.AddForce 方法来让敌人向玩家移动。你可以根据敌人和玩家之间的距离来调整敌人移动速度。 5. 如果敌人不能看到玩家,就让敌人继续巡逻。你可以使用随机移动或者预设的路径来让敌人巡逻。 下面是一个简单的示例代码: ```csharp public class Enemy : MonoBehaviour { public Transform player; public float moveSpeed = 5f; public float maxDistance = 10f; public float minDistance = 2f; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { float distance = Vector3.Distance(transform.position, player.position); if (distance <= maxDistance && distance >= minDistance) { RaycastHit2D hit = Physics2D.Linecast(transform.position, player.position, LayerMask.GetMask("Obstacle")); if (hit.collider == null) { Vector2 direction = (player.position - transform.position).normalized; rb.AddForce(direction * moveSpeed); } } } } ``` 在这个示例代码中,我们首先获取了敌人和玩家的位置信息。然后在 Update 方法中判断敌人和玩家之间的距离是否在最大距离和最小距离之间。如果在这个范围内,就使用 Linecast 方法判断敌人和玩家之间是否有障碍物。如果没有障碍物,就让敌人向玩家移动。 需要注意的是,我们在 AddForce 方法中使用了 normalized 方法来获取一个单位向量。这个向量表示了敌人到玩家的方向,并且长度为 1。我们将这个向量乘以 moveSpeed,就可以得到一个向玩家移动的速度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值