移动和旋转状态同步

This commit is contained in:
2025-09-11 23:44:02 +08:00
parent 20a9815359
commit 57d360f922
2 changed files with 23 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ namespace NBF.Fishing2
{
public class CharacterControllerComponent : Entity
{
public bool IsSelf;
public CharacterController characterController;
public readonly Queue<MoveState> MoveStateQueue = new Queue<MoveState>();
@@ -26,7 +27,7 @@ namespace NBF.Fishing2
// 插值平滑参数
public float positionLerpSpeed = 10f;
public float rotationLerpSpeed = 10f;
#region System
@@ -35,6 +36,7 @@ namespace NBF.Fishing2
protected override void Destroy(CharacterControllerComponent self)
{
self.characterController = null;
self.IsSelf = false;
var mapUnit = self.Parent as MapUnit;
if (mapUnit.IsSelf())
{
@@ -47,7 +49,6 @@ namespace NBF.Fishing2
inputComponent.OnPlayerValueCanceled -= self.OnPlayerValueCanceled;
inputComponent.OnPlayerValuePerformed -= self.OnPlayerValuePerformed;
}
}
}
}
@@ -65,6 +66,7 @@ namespace NBF.Fishing2
var mapUnit = self.Parent as MapUnit;
if (mapUnit.IsSelf())
{
self.IsSelf = true;
var inputComponent = self.Scene.GetComponent<InputComponent>();
inputComponent.OnPlayerPerformed += self.OnPlayerCanceled;
inputComponent.OnPlayerPerformed += self.OnPlayerPerformed;
@@ -123,6 +125,8 @@ namespace NBF.Fishing2
private void OnPlayerValuePerformed(InputAction.CallbackContext context)
{
// var mapUnit = Parent as MapUnit;
// Log.Info($"OnPlayerValuePerformed IsSelf={mapUnit.IsSelf()} id={mapUnit.Id}");
var name = context.action.name;
if (name == InputDef.Player.Move)
{
@@ -150,9 +154,11 @@ namespace NBF.Fishing2
if (!isStop)
{
// 获取角色当前的右向和前向(在水平面上)
Vector3 characterRight = Vector3.ProjectOnPlane(characterController.transform.right, Vector3.up).normalized;
Vector3 characterForward = Vector3.ProjectOnPlane(characterController.transform.forward, Vector3.up).normalized;
Vector3 characterRight =
Vector3.ProjectOnPlane(characterController.transform.right, Vector3.up).normalized;
Vector3 characterForward = Vector3.ProjectOnPlane(characterController.transform.forward, Vector3.up)
.normalized;
// 根据角色朝向计算实际移动方向
movementDirection = characterForward * movementInput.y + characterRight * movementInput.x;
}
@@ -225,7 +231,7 @@ namespace NBF.Fishing2
// 设置目标旋转用于插值
targetRotation = Quaternion.LookRotation(rotation);
}
#endregion
#region Move
@@ -280,12 +286,6 @@ namespace NBF.Fishing2
targetPosition += movement;
characterController.Move(movement);
// 如果移动方向不为零,可以自动更新朝向
if (currentMoveState.moveDirection != Vector3.zero)
{
// networkFacingDirection = currentMoveState.moveDirection;
targetRotation = Quaternion.LookRotation(currentMoveState.moveDirection);
}
}
// 添加位置和旋转插值方法
@@ -297,14 +297,14 @@ namespace NBF.Fishing2
{
// 插值位置
characterController.transform.position = Vector3.Lerp(
characterController.transform.position,
targetPosition,
characterController.transform.position,
targetPosition,
positionLerpSpeed * Time.deltaTime);
// 插值旋转 - 使用更平滑的插值方法
characterController.transform.rotation = Quaternion.Slerp(
characterController.transform.rotation,
targetRotation,
characterController.transform.rotation,
targetRotation,
rotationLerpSpeed * Time.deltaTime);
}
}
@@ -322,7 +322,7 @@ namespace NBF.Fishing2
private bool isRotating = false;
[Tooltip("视角旋转敏感度")] public Vector2 sensitivity = new Vector2(0.015f, 0.015f);
private void UpdatePlayerRotation(Vector2 lookValue)
{
// Look
@@ -346,6 +346,7 @@ namespace NBF.Fishing2
// 检查朝向同步
private void CheckRotationSync()
{
if(!IsSelf) return;
rotationSyncTimer += Time.deltaTime;
if (rotationSyncTimer >= rotationSyncInterval)
{

View File

@@ -1,6 +1,7 @@
using NBC;
using NBC.Entitas;
using NBC.Entitas.Interface;
using RootMotion.FinalIK;
using UnityEngine;
namespace NBF.Fishing2
@@ -15,6 +16,7 @@ namespace NBF.Fishing2
public Transform Transform { get; set; }
public Animator Animator { get; set; }
public LookAtIK LookAtIK { get; set; }
public async FTask InitUnityObject()
{
@@ -22,7 +24,9 @@ namespace NBF.Fishing2
GameObject = gameObject;
Transform = gameObject.transform;
Animator = gameObject.GetComponent<Animator>();
LookAtIK = gameObject.GetComponent<LookAtIK>();
Parent.AddComponent<CharacterControllerComponent>();
LookAtIK.enabled = false;
}