Unity 麦扣 x 勇士传说 全解析 之 怪物基类(2)(附各模块知识的链接,零基础也包学会的牢弟)(案例难度:★★☆☆☆)

1.怪物的动画逻辑一览

2.怪物的受伤死亡逻辑一览

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

public class Monster : MonoBehaviour
{
    [Header("速度")]
    public float normalSpeed;
    public float chaseSpeed;
    public float currentSpeed;
    [Header("面朝向")]
    protected Vector3 headFor;
    [Header("等待计时器")]
    public float waitTime;
    public float waitClock;
    public bool waitState;
    [Header("组件")]
    protected Rigidbody2D rb;
    protected Animator animator;
    protected PhysicsCheck physicscheck;

    private bool isWalk;
    private bool run;

    //当前移动
    private Transform attacker;
    //怪物受伤
    private bool isHurt;
    //受伤受到击退的力
    public float hurtForce;
    //死亡
    public bool isDead;
    private void Awake() {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        physicscheck = GetComponent<PhysicsCheck>();
       
    }
    private void Update() {

        //实时面朝向
        headFor = new Vector3(-this.transform.localScale.x,0,0);
        if ((headFor.x > 0 && physicscheck.isRightWall) || (headFor.x < 0 && physicscheck.isLeftWall)) {
            waitState = true;
            animator.SetBool("walk", false);
        }
        //延迟转身
        WaitAndTurn();
    }
    private void FixedUpdate() {
        if (!isHurt&&!isDead)
        {
            Move();
        }
       
    }
    //移动方法
    protected virtual void Move()
        {
        //移动算式
        rb.velocity=new Vector2( currentSpeed* headFor.x*Time.deltaTime,rb.velocity.y);
        if(!(physicscheck.isLeftWall&& physicscheck.isRightWall))
         {isWalk = true;
        animator.SetBool("walk", isWalk); }
        else
            {
            isWalk = false;
        }
    }
    //撞墙等待转身方法
    protected void WaitAndTurn()
        { 
    if(waitState)
            {
            waitClock -= Time.deltaTime;
            if(waitClock<= 0)
                {
                waitState=false;
                waitClock = waitTime; // 重置计时器
                this.transform.localScale = new Vector3(headFor.x,1,1);
            }       
        }
    
    }
    //受击后转身
    public void OnTakeDamage(Transform attcakTrans)
        {
        attacker = attcakTrans;
        if (attcakTrans.position.x - this.transform.position.x > 0)
            transform.localScale = new Vector3(-1, 1, 1);
        if (attcakTrans.position.x - this.transform.position.x < 0)
            transform.localScale = new Vector3(1, 1, 1);
        //受伤动画
        isHurt =true;
        animator.SetTrigger("IsHurt");
        //加力方向
        Vector2 dir = new Vector2(this.transform.position.x- attcakTrans.transform.position.x,0).normalized;
        StartCoroutine(OnHurt(dir));
    }
    private IEnumerator OnHurt(Vector2 dir)
        {
        rb.velocity = Vector2.zero; // 重置速度
        rb.AddForce(dir * hurtForce, ForceMode2D.Impulse);
        Debug.Log("Force applied: " + (dir * hurtForce)); // 添加调试信息
        yield return new WaitForSeconds(0.5f);
        isHurt = false;
    }
    public void Ondead() {
        isDead = true;
        animator.SetBool("dead",isDead);        
    }
    //死亡后销毁怪物
    public void DestoryMonseter()
        {
        Destroy(this.gameObject);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值