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);}
}