移动控制修改
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using NBC;
|
using NBC;
|
||||||
using NBC.Entitas;
|
using NBC.Entitas;
|
||||||
using NBC.Entitas.Interface;
|
using NBC.Entitas.Interface;
|
||||||
@@ -20,14 +20,14 @@ namespace NBF.Fishing2
|
|||||||
private Vector3 networkPosition;
|
private Vector3 networkPosition;
|
||||||
// private Vector3 networkFacingDirection;
|
// private Vector3 networkFacingDirection;
|
||||||
|
|
||||||
|
// 添加目标位置和旋转用于插值
|
||||||
|
private Vector3 targetPosition;
|
||||||
|
private Quaternion targetRotation;
|
||||||
|
|
||||||
// 插值平滑参数
|
// 插值平滑参数
|
||||||
public float positionLerpSpeed = 10f;
|
public float positionLerpSpeed = 10f;
|
||||||
public float rotationLerpSpeed = 10f;
|
public float rotationLerpSpeed = 10f;
|
||||||
|
|
||||||
|
|
||||||
[Tooltip("视角旋转敏感度")] public Vector2 sensitivity = new Vector2(0.015f, 0.015f);
|
|
||||||
public float minPitch = -40.0f;
|
|
||||||
public float maxPitch = 75.0f;
|
|
||||||
|
|
||||||
#region System
|
#region System
|
||||||
|
|
||||||
@@ -60,6 +60,9 @@ namespace NBF.Fishing2
|
|||||||
var unitUnityComponent = self.Parent.GetComponent<UnitUnityComponent>();
|
var unitUnityComponent = self.Parent.GetComponent<UnitUnityComponent>();
|
||||||
self.characterController = unitUnityComponent.GameObject.GetComponent<CharacterController>();
|
self.characterController = unitUnityComponent.GameObject.GetComponent<CharacterController>();
|
||||||
self.lastSyncedFacing = self.characterController.transform.forward;
|
self.lastSyncedFacing = self.characterController.transform.forward;
|
||||||
|
// 初始化目标位置和旋转
|
||||||
|
self.targetPosition = self.characterController.transform.position;
|
||||||
|
self.targetRotation = self.characterController.transform.rotation;
|
||||||
var mapUnit = self.Parent as MapUnit;
|
var mapUnit = self.Parent as MapUnit;
|
||||||
if (mapUnit.IsSelf())
|
if (mapUnit.IsSelf())
|
||||||
{
|
{
|
||||||
@@ -79,6 +82,8 @@ namespace NBF.Fishing2
|
|||||||
{
|
{
|
||||||
self.ProcessMoveStates();
|
self.ProcessMoveStates();
|
||||||
self.CheckRotationSync();
|
self.CheckRotationSync();
|
||||||
|
// 插值更新位置和旋转
|
||||||
|
self.InterpolatePositionAndRotation();
|
||||||
var mapUnit = self.Parent as MapUnit;
|
var mapUnit = self.Parent as MapUnit;
|
||||||
mapUnit.Position = self.characterController.transform.position;
|
mapUnit.Position = self.characterController.transform.position;
|
||||||
mapUnit.Rotation = self.characterController.transform.rotation;
|
mapUnit.Rotation = self.characterController.transform.rotation;
|
||||||
@@ -170,7 +175,6 @@ namespace NBF.Fishing2
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="startPosition"></param>
|
/// <param name="startPosition"></param>
|
||||||
/// <param name="moveDirection"></param>
|
/// <param name="moveDirection"></param>
|
||||||
/// <param name="facingDirection"></param>
|
|
||||||
/// <param name="speed"></param>
|
/// <param name="speed"></param>
|
||||||
/// <param name="timestamp"></param>
|
/// <param name="timestamp"></param>
|
||||||
public void OnServerStartMove(Vector3 startPosition, Vector3 moveDirection, float speed, double timestamp)
|
public void OnServerStartMove(Vector3 startPosition, Vector3 moveDirection, float speed, double timestamp)
|
||||||
@@ -192,7 +196,6 @@ namespace NBF.Fishing2
|
|||||||
/// 接收服务器移动停止通知
|
/// 接收服务器移动停止通知
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="finalPosition"></param>
|
/// <param name="finalPosition"></param>
|
||||||
/// <param name="finalFacing"></param>
|
|
||||||
/// <param name="timestamp"></param>
|
/// <param name="timestamp"></param>
|
||||||
public void OnServerStopMove(Vector3 finalPosition, double timestamp)
|
public void OnServerStopMove(Vector3 finalPosition, double timestamp)
|
||||||
{
|
{
|
||||||
@@ -212,7 +215,8 @@ namespace NBF.Fishing2
|
|||||||
|
|
||||||
public void OnServerLook(Vector3 rotation, double timestamp)
|
public void OnServerLook(Vector3 rotation, double timestamp)
|
||||||
{
|
{
|
||||||
|
// 设置目标旋转用于插值
|
||||||
|
targetRotation = Quaternion.LookRotation(rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -250,6 +254,7 @@ namespace NBF.Fishing2
|
|||||||
private void StartMovement(MoveState moveState)
|
private void StartMovement(MoveState moveState)
|
||||||
{
|
{
|
||||||
networkPosition = moveState.startPosition;
|
networkPosition = moveState.startPosition;
|
||||||
|
targetPosition = moveState.startPosition;
|
||||||
|
|
||||||
Debug.Log($"开始移动 - 位置: {moveState.startPosition}");
|
Debug.Log($"开始移动 - 位置: {moveState.startPosition}");
|
||||||
}
|
}
|
||||||
@@ -257,7 +262,7 @@ namespace NBF.Fishing2
|
|||||||
private void StopMovement(MoveState moveState)
|
private void StopMovement(MoveState moveState)
|
||||||
{
|
{
|
||||||
networkPosition = moveState.startPosition;
|
networkPosition = moveState.startPosition;
|
||||||
characterController.transform.position = networkPosition;
|
targetPosition = moveState.startPosition;
|
||||||
Debug.Log($"停止移动 - 最终位置: {moveState.startPosition}");
|
Debug.Log($"停止移动 - 最终位置: {moveState.startPosition}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,12 +273,35 @@ namespace NBF.Fishing2
|
|||||||
|
|
||||||
Vector3 movement = movementDirection * currentMoveState.moveSpeed * Time.deltaTime;
|
Vector3 movement = movementDirection * currentMoveState.moveSpeed * Time.deltaTime;
|
||||||
networkPosition += movement;
|
networkPosition += movement;
|
||||||
|
targetPosition = networkPosition;
|
||||||
characterController.Move(movement);
|
characterController.Move(movement);
|
||||||
|
|
||||||
// 如果移动方向不为零,可以自动更新朝向
|
// 如果移动方向不为零,可以自动更新朝向
|
||||||
if (currentMoveState.moveDirection != Vector3.zero)
|
if (currentMoveState.moveDirection != Vector3.zero)
|
||||||
{
|
{
|
||||||
// networkFacingDirection = currentMoveState.moveDirection;
|
// networkFacingDirection = currentMoveState.moveDirection;
|
||||||
|
targetRotation = Quaternion.LookRotation(currentMoveState.moveDirection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加位置和旋转插值方法
|
||||||
|
private void InterpolatePositionAndRotation()
|
||||||
|
{
|
||||||
|
// 只对非本地玩家进行插值
|
||||||
|
var mapUnit = Parent as MapUnit;
|
||||||
|
if (!mapUnit.IsSelf())
|
||||||
|
{
|
||||||
|
// 插值位置
|
||||||
|
characterController.transform.position = Vector3.Lerp(
|
||||||
|
characterController.transform.position,
|
||||||
|
targetPosition,
|
||||||
|
positionLerpSpeed * Time.deltaTime);
|
||||||
|
|
||||||
|
// 插值旋转
|
||||||
|
characterController.transform.rotation = Quaternion.Slerp(
|
||||||
|
characterController.transform.rotation,
|
||||||
|
targetRotation,
|
||||||
|
rotationLerpSpeed * Time.deltaTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,26 +313,31 @@ namespace NBF.Fishing2
|
|||||||
public float minRotationChange = 10f;
|
public float minRotationChange = 10f;
|
||||||
|
|
||||||
private Vector3 _rotationInput = Vector3.zero;
|
private Vector3 _rotationInput = Vector3.zero;
|
||||||
private float _cameraPitch;
|
|
||||||
private float rotationSyncTimer;
|
private float rotationSyncTimer;
|
||||||
private Vector3 lastSyncedFacing;
|
private Vector3 lastSyncedFacing;
|
||||||
|
|
||||||
|
[Tooltip("视角旋转敏感度")] public Vector2 sensitivity = new Vector2(0.015f, 0.015f);
|
||||||
|
|
||||||
private void UpdatePlayerRotation(Vector2 lookValue)
|
private void UpdatePlayerRotation(Vector2 lookValue)
|
||||||
{
|
{
|
||||||
// Look
|
// Look
|
||||||
// Vector2 lookInput = lookValue * sensitivity;
|
Vector2 lookInput = lookValue * sensitivity;
|
||||||
//
|
|
||||||
// if (lookInput.x != 0.0f)
|
// 处理水平旋转 (Yaw)
|
||||||
// {
|
if (lookInput.x != 0.0f)
|
||||||
// _rotationInput.y += lookInput.x;
|
{
|
||||||
// }
|
_rotationInput.y += lookInput.x;
|
||||||
//
|
}
|
||||||
// // Player.CameraRoot.transform.localRotation = Quaternion.Euler(_cameraPitch, 0.0f, 0.0f);
|
|
||||||
// if (_rotationInput != Vector3.zero)
|
|
||||||
// {
|
|
||||||
// characterController.transform.rotation *= Quaternion.Euler(_rotationInput);
|
// 应用旋转到角色和相机
|
||||||
// _rotationInput = Vector3.zero;
|
if (_rotationInput != Vector3.zero)
|
||||||
// }
|
{
|
||||||
|
characterController.transform.rotation *= Quaternion.Euler(0, _rotationInput.y, 0);
|
||||||
|
_rotationInput = Vector3.zero;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查朝向同步
|
// 检查朝向同步
|
||||||
@@ -326,4 +359,4 @@ namespace NBF.Fishing2
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user