This commit is contained in:
MuHua-123
2025-03-07 22:46:13 +08:00
parent 5e819d5257
commit 57a8a6e9a1
83 changed files with 235 additions and 977 deletions
@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 运动控制器
/// </summary>
public abstract class Movement : MonoBehaviour {
public float moveSpeed = 5.0f; // 最大移动速度
public float acceleration = 20.0f; // 加速度
public float currentSpeed = 0.0f; // 当前速度
public Vector3 front; // 面向
public abstract bool UpdateMove(Vector3 position);
public abstract Vector3 RandomTargetPosition();
public abstract void StopMoving();
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d034bee659058394d8641aad1f9e7023
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
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() {
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9eb06e286a5289a439f220c0a3656568
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 标准运动实现
/// </summary>
public class MovementStandard : Movement {
public override bool UpdateMove(Vector3 position) {
// 计算相对于世界坐标系的移动方向
Vector3 moveDirection = (position - transform.position).normalized;
float distance = Vector3.Distance(transform.position, position);
// 平滑加速和减速
currentSpeed = distance > 0.2f
? Mathf.MoveTowards(currentSpeed, moveSpeed, acceleration * Time.deltaTime)
: Mathf.MoveTowards(currentSpeed, 0, acceleration * Time.deltaTime);
// 移动玩家
transform.Translate(moveDirection * currentSpeed * Time.deltaTime, Space.World);
// 如果有移动输入,则更新玩家的朝向
if (distance != 0) {
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, moveSpeed * Time.deltaTime * 100);
}
// 计算转向向量
Vector3 localMoveDirection = transform.InverseTransformDirection(moveDirection * currentSpeed);
localMoveDirection = localMoveDirection.normalized;
// 对localMoveDirection的x和z进行分类处理
float moveX = Convert.ToInt32(localMoveDirection.x);
float moveZ = Convert.ToInt32(localMoveDirection.z);
front = new Vector3(moveX, 0, moveZ);
// 如果到达目标位置,返回 true
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;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 699b617ae51a79a4390529e4e03405a2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: