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

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

2024/12/23 9:32:24 来源:https://blog.csdn.net/2301_77947509/article/details/141299159  浏览:    关键词: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);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com