[2021.10.28]<呆头熊的开发日记>怪物AI(4)

举盾牌小怪:(标签为Shield,而非Enemy)

1.固定区域巡逻;

2.前后无法攻击、受伤;

3.发现玩家主动攻击;

4.玩家仅能下落攻击对其造成伤害。

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

public class ShieldM : Enemy
{
    private Rigidbody2D rb;

    public Transform leftpoint, rightpoint;

    public float Speed;
    private bool Faceleft = true;
    private Transform target;
    [SerializeField] private float attackRange; 
    
    private Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        transform.DetachChildren();  //断绝左右节点与怪的父子关系(使预设好的左右节点能固定位置)
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        anim = GetComponent<Animator>(); 
    }

    // Update is called once per frame
    void Update()
    {
        Movment();
        if (Vector2.Distance(transform.position, target.position) < attackRange)
        {
            anim.SetBool("isAttack", true); 
        }
        else
        {
            anim.SetBool("isAttack", false);
        }
    }

    void Movment()
    {
        anim.SetTrigger("IDLE");
        if (Faceleft)
        {
            rb.velocity = new Vector2(-Speed, rb.velocity.y);
            if (transform.position.x < leftpoint.position.x)
            {
                transform.localScale = new Vector3(-1, 1, 1);
                Faceleft = false;
            }
        }
        else
        {
            rb.velocity = new Vector2(Speed, rb.velocity.y);
            if (transform.position.x > rightpoint.position.x)
            {
                transform.localScale = new Vector3(1, 1, 1);
                Faceleft = true;
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值