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