更新角色包

This commit is contained in:
MuHua-123
2025-03-18 16:29:34 +08:00
parent e4d10c15c6
commit 94e29db2cb
52 changed files with 294 additions and 653 deletions
@@ -1,25 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class AnimatorStandard : IAnimator {
private string layer;
private string current;
private Animator animator;
private void Awake() => animator = GetComponent<Animator>();
public override void Transition(string name) {
if (current == name) { animator.Play(name); }
else { animator.CrossFade(name, 0.1f); }
current = name;
// 判断当前动画,如果不是相同动画就增加过渡
// AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);
// Debug.Log($"{currentState.normalizedTime} , {this.kinesis.AnimName}");
}
public override void SetFloat(string name, float value) => animator.SetFloat(name, value);
}
}
@@ -1,13 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
[RequireComponent(typeof(Animator))]
public abstract class IAnimator : MonoBehaviour {
/// <summary> 动画过渡 </summary>
public abstract void Transition(string name);
/// <summary> 设置参数 </summary>
public abstract void SetFloat(string name, float value);
}
}
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: ddf575a280d8de544894143ab841f4d7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,16 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class CharacterPlayer : ICharacter {
private void Awake() => Transitionkinesis(new KinesisIdle(this));
public override void Transitionkinesis(Ikinesis kinesis) {
if (currentKinesis != null && !currentKinesis.Transition(kinesis)) { return; }
currentKinesis = kinesis;
currentKinesis.Startkinesis();
}
}
}
@@ -1,21 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class ICharacter : MonoBehaviour {
public Ikinesis currentKinesis;
public void Update() => currentKinesis.Update();
/// <summary> 过渡动作 </summary>
public abstract void Transitionkinesis(Ikinesis kinesis);
/// <summary> 触发动画特效 </summary>
public virtual void AnimationEffects() => currentKinesis.AnimationEffects();
/// <summary> 动画结束 </summary>
public virtual void AnimationEnd() => currentKinesis.AnimationEnd();
/// <summary> 动画退出 </summary>
public virtual void AnimationExit() => currentKinesis.AnimationExit();
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 9523e030f852bd0488ea72f52125fc67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f0aeb4e44882d754a94ca38a40711e98
guid: 6636b9d6db0317e43a61b41ac2b2297d
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作动画控制器
/// </summary>
[RequireComponent(typeof(Animator))]
public class KinesisAnimator : MonoBehaviour {
protected int layerIndex;
protected string current;
protected Animator animator;
protected IKinesis kinesis;
public virtual void Awake() => animator = GetComponent<Animator>();
/// <summary> 设置动作 </summary>
public virtual void SetKinesis(IKinesis kinesis) => this.kinesis = kinesis;
/// <summary> 动画过渡 </summary>
public virtual void Transition(int layerIndex, string name, float normalizedTransitionDuration = 0.1f) {
animator.SetLayerWeight(this.layerIndex, 0);
animator.SetLayerWeight(layerIndex, 1);
this.layerIndex = layerIndex;
Transition(name, normalizedTransitionDuration);
}
/// <summary> 动画过渡 </summary>
public virtual void Transition(string name, float normalizedTransitionDuration = 0.1f) {
if (current == name) { animator.Play(name); }
else { animator.CrossFade(name, normalizedTransitionDuration); }
current = name;
}
/// <summary> 设置参数 </summary>
public virtual void SetBool(string name, bool value) => animator.SetBool(name, value);
/// <summary> 设置参数 </summary>
public virtual void SetFloat(string name, float value) => animator.SetFloat(name, value);
/// <summary> 触发动画特效 </summary>
public virtual void AnimationEffects() => kinesis?.AnimationEffects();
/// <summary> 动画结束(有后摇) </summary>
public virtual void AnimationEnd() => kinesis?.AnimationEnd();
/// <summary> 动画退出(无后摇) </summary>
public virtual void AnimationExit() => kinesis?.AnimationExit();
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 28866a6768c3e7441aa12caefd6217b5
guid: e3ceef7224b9b094aadaa9b3a0238648
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作控制器
/// </summary>
public class KinesisController : MonoBehaviour {
public KinesisAnimator animator;
public KinesisMovement movement;
private IKinesis currentKinesis;
public virtual void Awake() => TransitionKinesis(new KinesisIdle());
public virtual void Update() => currentKinesis?.UpdateKinesis();
/// <summary> 动作过渡 </summary>
public virtual void TransitionKinesis(IKinesis kinesis) {
//不可以转换
if (currentKinesis != null && !currentKinesis.Transition(kinesis)) { return; }
//进行转换
currentKinesis?.FinishKinesis();
currentKinesis = kinesis;
currentKinesis?.StartKinesis();
animator?.SetKinesis(currentKinesis);
movement?.SetKinesis(currentKinesis);
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e64668e8630d6c149bd637156aa1c42d
guid: 2c1979e13b26c7b48a7a4690da154535
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,100 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作运动控制器
/// </summary>
[RequireComponent(typeof(CharacterController))]
public class KinesisMovement : MonoBehaviour {
public float moveSpeed = 5;// 移动速度
public float acceleration = 10.0f;// 加速度
[Range(0.0f, 0.3f)]
public float rotationSmoothTime = 0.12f;// 旋转平滑
protected float currentSpeed;// 当前速度
protected Vector2 moveDirection;// 移动方向
protected float animationBlend;// 动画混合速度
protected float targetRotation = 0.0f;// 旋转目标
protected float rotationVelocity;// 旋转速度
protected float verticalVelocity;// 垂直速度
protected IKinesis kinesis;// 当前动作
protected KinesisAnimator animator;// 动作动画控制器
protected CharacterController controller;// 角色控制器
public virtual bool IsStop => currentSpeed == 0;
public virtual void Awake() {
animator = GetComponent<KinesisAnimator>();
controller = GetComponent<CharacterController>();
}
public virtual void Update() {
PlanarMovement();
}
/// <summary> 设置动作 </summary>
public virtual void SetKinesis(IKinesis kinesis) => this.kinesis = kinesis;
/// <summary> 设置方向 </summary>
public virtual void SetDirection(Vector2 moveDirection) => this.moveDirection = moveDirection;
/// <summary> 停止移动 </summary>
public virtual void StopMovement() {
currentSpeed = 0;
moveDirection = Vector2.zero;
animationBlend = 0;
animator?.SetFloat("MoveSpeed", animationBlend);
}
/// <summary> 平面移动 </summary>
public virtual void PlanarMovement() {
// 设定目标速度
float targetSpeed = moveSpeed;
// 一种简单的加速和减速设计,易于拆卸、更换或迭代
// 如果没有输入,将目标速度设置为0
if (moveDirection == Vector2.zero && currentSpeed == 0) { return; }
if (moveDirection == Vector2.zero) targetSpeed = 0.0f;
// 当前水平速度的参考
// float currentHorizontalSpeed = new Vector3(controller.velocity.x, 0.0f, controller.velocity.z).magnitude;
// float speedOffset = 0.1f;
// 加速或减速至目标速度
// if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset) {
// 产生弯曲的结果,而不是线性的结果,从而产生更有机的速度变化
// 注意Lerp中的T是夹紧的,所以我们不需要夹紧我们的速度
currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, Time.deltaTime * acceleration);
// round speed to 3 decimal places
currentSpeed = Mathf.Round(currentSpeed * 1000f) / 1000f;
// }
// else { currentSpeed = targetSpeed; }
animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * acceleration);
if (animationBlend < 0.01f) animationBlend = 0f;
// 使输入方向标准化
Vector3 inputDirection = new Vector3(moveDirection.x, 0.0f, moveDirection.y).normalized;
// 如果有移动输入,则在玩家移动时旋转玩家
if (moveDirection != Vector2.zero) {
targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref rotationVelocity, rotationSmoothTime);
// 相对于相机位置旋转到面向输入方向
transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
}
Vector3 targetDirection = Quaternion.Euler(0.0f, targetRotation, 0.0f) * Vector3.forward;
// 移动
controller.Move(targetDirection.normalized * (currentSpeed * Time.deltaTime) + new Vector3(0.0f, verticalVelocity, 0.0f) * Time.deltaTime);
// 如果使用角色,请更新动画师
animator?.SetFloat("MoveSpeed", animationBlend);
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 8394161d0e9670f4a83246f5a5da722b
guid: 61ef13b926e462442920d0ba932e8e27
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -1,43 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace MuHua {
public class ControllerPlayer : MonoBehaviour {
public Transform cameraController; // 相机对象
public CharacterPlayer character;
private Vector2 moveInput;
private void Update() {
if (moveInput == Vector2.zero) { return; }
// 获取相机的前向和右向
Vector3 cameraForward = cameraController.transform.forward;
Vector3 cameraRight = cameraController.transform.right;
// 忽略相机的y轴
cameraForward.y = 0;
cameraRight.y = 0;
// 归一化向量
cameraForward.Normalize();
cameraRight.Normalize();
// 计算相对于相机的移动方向
Vector3 moveDirection = (cameraForward * moveInput.y + cameraRight * moveInput.x).normalized;
// 相对于玩家的移动方向
Vector3 position = character.transform.position + new Vector3(moveDirection.x, 0, moveDirection.z) * 0.5f;
KinesisMove kinesis = new KinesisMove(character, position);
character.Transitionkinesis(kinesis);
}
#region
public void OnMove(InputValue inputValue) {
// 获取移动输入
moveInput = inputValue.Get<Vector2>();
}
#endregion
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 73bafdf07b08fc246ab173a5e555629e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class EffectsLaunch : MonoBehaviour {
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 36eaafdfeb2c4f44f82504e36afe1cda
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 91660569267b53545ab6e688c384eae2
guid: a373c751fa5625249af75654ab69aec4
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 角色动作
/// </summary>
public interface IKinesis {
/// <summary> 动作过渡 </summary>
public bool Transition(IKinesis kinesis);
/// <summary> 开始动作 </summary>
public void StartKinesis();
/// <summary> 更新动作 </summary>
public void UpdateKinesis();
/// <summary> 完成动作 </summary>
public void FinishKinesis();
/// <summary> 触发动画特效 </summary>
public void AnimationEffects();
/// <summary> 动画结束(有后摇) </summary>
public void AnimationEnd();
/// <summary> 动画退出(无后摇) </summary>
public void AnimationExit();
}
}
-8
View File
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: ff61e8e49afe14548a5ec4151e14ab34
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,25 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 角色动作
/// </summary>
public abstract class Ikinesis {
/// <summary> 持续更新 </summary>
public virtual void Update() { }
/// <summary> 动作过渡 </summary>
public abstract bool Transition(Ikinesis kinesis);
public virtual void Startkinesis() { }
/// <summary> 触发动画特效 </summary>
public virtual void AnimationEffects() { }
/// <summary> 动画结束 </summary>
public virtual void AnimationEnd() { }
/// <summary> 动画退出 </summary>
public virtual void AnimationExit() { }
}
}
@@ -1,22 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class KinesisAttack : Ikinesis {
public bool animEnd = false;
public string animName;
public IAnimator animator;
public KinesisAttack(ICharacter character, string name = "Attack01") {
animName = name;
animator = character.GetComponent<IAnimator>();
}
public override bool Transition(Ikinesis kinesis) => animEnd;
public override void Startkinesis() => animator.Transition(animName);
public override void AnimationEnd() => animEnd = true;
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 6c910b7b6bba29d4381c9bb1d615ff77
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,15 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class KinesisIdle : Ikinesis {
public IAnimator animator;
public KinesisIdle(ICharacter character) => animator = character.GetComponent<IAnimator>();
public override bool Transition(Ikinesis kinesis) => true;
public override void Startkinesis() => animator.Transition("Idle");
}
}
@@ -1,31 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class KinesisMove : Ikinesis {
public Vector3 position;
public IMovement movement;
public IAnimator animator;
public ICharacter character;
public KinesisMove(ICharacter character, Vector3 position) {
this.position = position;
this.character = character;
movement = character.GetComponent<IMovement>();
animator = character.GetComponent<IAnimator>();
}
public override bool Transition(Ikinesis kinesis) => true;
public override void Startkinesis() => animator.Transition("Move");
public override void Update() {
if (movement.UpdateMove(position, 5)) { character.Transitionkinesis(new KinesisIdle(character)); }
animator.SetFloat("MoveSpeed", movement.CurrentSpeed);
animator.SetFloat("MoveX", movement.Direction.x);
animator.SetFloat("MoveZ", movement.Direction.z);
}
}
}
@@ -1,14 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using MuHua;
using UnityEngine;
namespace MuHua {
public class KinesisTurn : Ikinesis {
public override bool Transition(Ikinesis kinesis) {
throw new System.NotImplementedException();
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 891f774ac729c45409682ff40c2d4615
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-8
View File
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 0559d32326453c64bb54d392731648a2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,33 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 有限状态机
/// </summary>
public abstract class IMachine : MonoBehaviour {
protected IMachineState currentState;
protected Dictionary<string, IMachineState> states = new Dictionary<string, IMachineState>();
protected virtual void Start() => InitializeStates();
protected virtual void Update() => currentState?.Update();
#region
protected abstract void InitializeStates();
protected virtual void RegisterState(string stateType, IMachineState state) {
if (!states.ContainsKey(stateType)) { states.Add(stateType, state); }
}
public virtual void ChangeState(string stateType) {
if (states.ContainsKey(stateType)) {
currentState?.Exit();
currentState = states[stateType];
currentState.Enter();
}
else {
Debug.LogWarning($"State {stateType} is not registered.");
}
}
#endregion
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 0cd74b3acde213f489a11a9e0ccd81e7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: a2af9ed6fc4426e49807173a25d3d2cb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,23 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 状态接口
/// </summary>
public abstract class IMachineState {
protected readonly IMachine machine;
public IMachineState(IMachine machine) => this.machine = machine;
/// <summary> 进入状态 </summary>
public abstract void Enter();
/// <summary> 更新状态 </summary>
public abstract void Update();
/// <summary> 退出状态 </summary>
public abstract void Exit();
/// <summary> 触发状态 </summary>
public abstract void Trigger();
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 18d0a64357fbec04d8fbd51d45c88e4b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
-8
View File
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: a359f8431aa19b846b623d9dd066bae3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,22 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 运动控制器
/// </summary>
public abstract class IMovement : MonoBehaviour {
/// <summary> 当前速度 </summary>
public abstract float CurrentSpeed { get; }
/// <summary> 当前方向 </summary>
public abstract Vector3 Direction { get; }
/// <summary> 更新移动 </summary>
public abstract bool UpdateMove(Vector3 position, float moveSpeed);
/// <summary> 获取随机位置 </summary>
public abstract Vector3 RandomTargetPosition();
/// <summary> 停止移动 </summary>
public abstract void StopMoving();
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d034bee659058394d8641aad1f9e7023
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,17 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public class MovementNavigation : MonoBehaviour {
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 9eb06e286a5289a439f220c0a3656568
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,115 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 标准运动实现
/// </summary>
public class MovementStandard : IMovement {
public Vector3 position;
public float moveSpeed = 5.0f; // 最大移动速度
public float acceleration = 20.0f; // 加速度
public float steeringSpeed = 180.0f; // 加速度
public float currentSpeed = 0.0f; // 当前速度
public Vector3 direction; // 面向
public CharacterController controller;
private float _targetRotation = 0.0f;
private float _rotationVelocity;
private float _verticalVelocity;
public override float CurrentSpeed => currentSpeed;
public override Vector3 Direction => direction;
private void Awake() => position = transform.position;
private void Update() {
// 计算相对于世界坐标系的移动方向
Vector3 moveDirection = (position - transform.position).normalized;
float distance = Vector3.Distance(transform.position, position);
// float targetSpeed = _input.sprint ? SprintSpeed : MoveSpeed;
float targetSpeed = moveSpeed;
// 注意:Vector2的==运算符使用近似值,因此不易出现浮点误差,并且比幅度便宜
// 如果没有输入,将目标速度设置为0
if (distance < 0.2f) { targetSpeed = 0.0f; }
// 角色当前水平速度的参考
//float currentHorizontalSpeed = new Vector3(controller.velocity.x, 0.0f, controller.velocity.z).magnitude;
float speedOffset = 0.1f;
//float inputMagnitude = moveDirection.magnitude;
// 加速或减速至目标速度
if (currentSpeed < targetSpeed - speedOffset || currentSpeed > targetSpeed + speedOffset) {
// 产生弯曲的结果,而不是线性的结果,从而产生更有机的速度变化
// 注意Lerp中的T是夹紧的,所以我们不需要夹紧我们的速度
currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, Time.deltaTime * acceleration);
// 四舍五入到小数点后3位
//currentSpeed = Mathf.Round(currentSpeed * 1000f) / 1000f;
}
else { currentSpeed = targetSpeed; }
// 注意:矢量2!=运算符使用近似值,因此不易出现浮点误差,而且比幅度便宜
// 如果有移动输入,则在玩家移动时旋转玩家
// if (moveDirection != Vector3.zero) {
// _targetRotation = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
// float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVelocity, steeringSpeed);
// // 相对于相机位置旋转到面向输入方向
// transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
// }
//Vector3 targetDirection = Quaternion.Euler(0.0f, _targetRotation, 0.0f) * Vector3.forward;
// 移动角色
// controller.Move(targetDirection.normalized * (currentSpeed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime);
// 平滑加速和减速
//currentSpeed = Mathf.MoveTowards(currentSpeed, moveSpeed, acceleration * Time.deltaTime);
// 移动玩家
//transform.Translate(moveDirection * currentSpeed * Time.deltaTime, Space.World);
controller.Move(moveDirection * currentSpeed * Time.deltaTime);
// 如果有移动输入,则更新玩家的朝向
if (distance > 0.1f) {
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, moveSpeed * Time.deltaTime * steeringSpeed);
}
// 计算转向向量
Vector3 localMoveDirection = transform.InverseTransformDirection(moveDirection * currentSpeed);
localMoveDirection = localMoveDirection.normalized;
// 对localMoveDirection的x和z进行分类处理
float moveX = Convert.ToInt32(localMoveDirection.x);
float moveZ = Convert.ToInt32(localMoveDirection.z);
direction = new Vector3(moveX, 0, moveZ);
}
public override bool UpdateMove(Vector3 position, float moveSpeed) {
this.position = position;
this.moveSpeed = moveSpeed;
// 如果到达目标位置,返回 true
float distance = Vector3.Distance(transform.position, position);
return distance < 0.05f;
}
public override Vector3 RandomTargetPosition() {
float randomX = UnityEngine.Random.Range(-10.0f, 10.0f);
float randomZ = UnityEngine.Random.Range(-10.0f, 10.0f);
return transform.position + new Vector3(randomX, 0, randomZ);
}
public override void StopMoving() {
currentSpeed = 0;
}
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 699b617ae51a79a4390529e4e03405a2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,9 +1,7 @@
{
"name": "MuHua.Character",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c"
],
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 7cd21ba4756d406468a37fe882d097ae
guid: 8c36f49b360d4c048aef47715ebb0616
folderAsset: yes
DefaultImporter:
externalObjects: {}
@@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 空闲动作
/// </summary>
public class KinesisIdle : IKinesis {
public bool Transition(IKinesis kinesis) => true;
public void StartKinesis() { }
public void UpdateKinesis() { }
public void FinishKinesis() { }
public void AnimationEffects() { }
public void AnimationEnd() { }
public void AnimationExit() { }
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 190d2b1706b7e0b42b9700950c4e2c18
guid: 99fdb0fe5f05b3b44bf010af88ab1f8f
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 移动动作
/// </summary>
public class KinesisMove : IKinesis {
public Vector3 position;
public Vector3 eulerAngles;
public Vector2 moveDirection;// 移动方向
public KinesisMovement movement;
public KinesisController controller;
public KinesisMove(KinesisController controller, Vector2 moveDirection) {
this.controller = controller;
this.moveDirection = moveDirection;
movement = controller.movement;
position = movement.transform.position;
eulerAngles = movement.transform.eulerAngles;
}
public KinesisMove(KinesisController controller, Vector2 moveDirection, Vector3 position, Vector3 eulerAngles) {
this.position = position;
this.eulerAngles = eulerAngles;
this.controller = controller;
this.moveDirection = moveDirection;
movement = controller.movement;
}
public bool Transition(IKinesis kinesis) {
KinesisMove move = kinesis as KinesisMove;
if (move == null) { return true; }
position = move.position;
eulerAngles = move.eulerAngles;
moveDirection = move.moveDirection;
movement.transform.position = position;
movement.transform.eulerAngles = eulerAngles;
movement.SetDirection(moveDirection);
return false;
}
public void StartKinesis() {
movement.transform.position = position;
movement.transform.eulerAngles = eulerAngles;
movement.SetDirection(moveDirection);
}
public void UpdateKinesis() {
if (!movement.IsStop) { return; }
controller.TransitionKinesis(new KinesisIdle());
}
public void FinishKinesis() {
movement.StopMovement();
}
public void AnimationEffects() { }
public void AnimationEnd() { }
public void AnimationExit() { }
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 7c4416dfc61926e439368a6fd3b26675
guid: 5c1bd98cf0e078140ab99d637e398ed1
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 1c84ca502025179419c2b8afb29080aa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class VisualField : MonoBehaviour {
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: e4d36e9e525a8a7429e5a7d9edf17cb2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
public abstract class VisualFieldStandard : VisualField {
}
}
@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 3595d54d26ddd5c47b4d4a3025935ecb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: