163 lines
3.8 KiB
C#
163 lines
3.8 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
|
|||
|
}
|
|||
|
public class CharacterAin : MonoBehaviour
|
|||
|
{
|
|||
|
private Animator ain;
|
|||
|
private float timer;
|
|||
|
private float RunSpeed;
|
|||
|
public CharacterState characterState;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2>
|
|||
|
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>
|
|||
|
/// <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>Ķ<EFBFBD><C4B6><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <param name="characterState">״̬ö<CCAC><C3B6></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)); // <20><><EFBFBD><EFBFBD> Lerp <20>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ֵϵ<D6B5><CFB5>
|
|||
|
ain.SetBool("IsOnGround", true);
|
|||
|
break;
|
|||
|
case CharacterState.SlowWalk:
|
|||
|
ain.SetInteger("State", 1);
|
|||
|
ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 0.4f, 0.1f)); // <20><>С<EFBFBD><D0A1><EFBFBD>ƶ<EFBFBD><C6B6>ٶ<EFBFBD>
|
|||
|
break;
|
|||
|
case CharacterState.walk:
|
|||
|
ain.SetInteger("State", 1);
|
|||
|
ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 0.6f, 0.1f)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>
|
|||
|
break;
|
|||
|
case CharacterState.run:
|
|||
|
ain.SetInteger("State", 1);
|
|||
|
ain.SetFloat("RunSpeed", Mathf.Lerp(RunSpeed, 2f, 0.1f)); // <20>ܲ<EFBFBD><DCB2>ٶ<EFBFBD>
|
|||
|
break;
|
|||
|
case CharacterState.jump:
|
|||
|
ain.SetBool("IsOnGround", false);
|
|||
|
ain.SetInteger("State", 2);
|
|||
|
break;
|
|||
|
case CharacterState.Dead:
|
|||
|
ain.SetInteger("State", 99);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ﵱǰ״̬
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public CharacterState GetCharacterState()
|
|||
|
{
|
|||
|
return this.characterState;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DFBC>⣬<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ڵ<EFBFBD><DAB5><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public bool IsOnGround()
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
//<2F>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>棬<EFBFBD>ָ<EFBFBD>վ<EFBFBD><D5BE>
|
|||
|
public void ReState()
|
|||
|
{
|
|||
|
|
|||
|
if (characterState == CharacterState.jump && IsOnGround())
|
|||
|
{
|
|||
|
SetPlayerState(CharacterState.idle);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD>ܶ<EFBFBD><DCB6><EFBFBD>
|
|||
|
/// </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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|