175 lines
4.0 KiB
C#
175 lines
4.0 KiB
C#
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
|
|
}
|
|
|
|
|
|
|
|
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<Animator>();
|
|
colExtents = GetComponent<Collider>().bounds.extents;
|
|
characterState = CharacterState.idle;
|
|
SetPlayerState(characterState);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 改变人物状态的动画
|
|
/// </summary>
|
|
/// <param name="characterState">状态枚举</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回人物当前状态
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public CharacterState GetCharacterState()
|
|
{
|
|
return this.characterState;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 球形射线检测,判断人物是否在地面
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 技能动画
|
|
/// </summary>
|
|
/// <param name="Skill"></param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
IEnumerator jiutimer(float timer)
|
|
{
|
|
yield return new WaitForSeconds(timer);
|
|
}
|
|
|
|
}
|