using System.Collections; using System.Collections.Generic; using UnityEngine; public enum CharacterState { idle, SlowWalk, walk, run, jump, Dead, } public enum CharacterSkill { NoSkill,//0 Watering, //1 jump,//2 Rescue,//3 OperateSuccess,//4 OperateFail//5 } public class CharacterAin : MonoBehaviour { private Animator ain; private float timer; private float RunSpeed; public CharacterState characterState; //玩家碰撞体 private Vector3 colExtents; // Start is called before the first frame update void Start() { Init(); } // Update is called once per frame void Update() { } private void FixedUpdate() { ReState(); } void Init() { ain = transform.GetComponent(); colExtents = GetComponent().bounds.extents; characterState = CharacterState.idle; SetPlayerState(characterState); } /// /// 改变人物状态的动画 /// /// 状态枚举 public void SetPlayerState(CharacterState characterState) { if (this.characterState == CharacterState.Dead) { return; } this.characterState = characterState; RunSpeed = ain.GetFloat("RunSpeed"); switch (this.characterState) { case CharacterState.idle: ain.SetInteger("State", 0); ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 0f, 0.1f)); // 控制 Lerp 的第三个参数为插值系数 ain.SetBool("IsOnGround", true); break; case CharacterState.SlowWalk: ain.SetInteger("State", 1); ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 0.4f, 0.1f)); // 更小的移动速度 break; case CharacterState.walk: ain.SetInteger("State", 1); ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 0.6f, 0.1f)); // 正常行走速度 break; case CharacterState.run: ain.SetInteger("State", 1); ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 2f, 0.1f)); // 跑步速度 break; case CharacterState.jump: ain.SetBool("IsOnGround", false); ain.SetInteger("State", 2); break; case CharacterState.Dead: ain.SetInteger("State", 99); break; } } /// /// 返回人物当前状态 /// /// public CharacterState GetCharacterState() { return this.characterState; } /// /// 球形射线检测,判断人物是否在地面 /// /// public bool IsOnGround() { //玩家碰撞体的中位发射射线 Ray ray = new Ray(this.transform.position + Vector3.up * (2 * colExtents.x), Vector3.down); if (Physics.SphereCast(ray, colExtents.x, colExtents.x + 0.2f)) { return true; } return false; } //回到地面,恢复站立 public void ReState() { if (characterState == CharacterState.jump && IsOnGround()) { SetPlayerState(CharacterState.idle); } } /// /// 技能动画 /// /// public void SetSkillAin(CharacterSkill Skill) { switch (Skill) { case CharacterSkill.NoSkill: ain.SetInteger("Skill", 0); break; case CharacterSkill.Watering: ain.SetInteger("Skill", 1); break; case CharacterSkill.jump: ain.SetInteger("Skill", 2); break; case CharacterSkill.Rescue: ain.SetInteger("Skill",3); break; case CharacterSkill.OperateSuccess: ain.SetInteger("Skill", 4); break; case CharacterSkill.OperateFail: ain.SetInteger("Skill", 5); break; } } IEnumerator jiutimer(float timer) { yield return new WaitForSeconds(timer); } }