移动和旋转状态同步
This commit is contained in:
@@ -10,6 +10,7 @@ namespace NBF.Fishing2
|
|||||||
{
|
{
|
||||||
public class CharacterControllerComponent : Entity
|
public class CharacterControllerComponent : Entity
|
||||||
{
|
{
|
||||||
|
public bool IsSelf;
|
||||||
public CharacterController characterController;
|
public CharacterController characterController;
|
||||||
|
|
||||||
public readonly Queue<MoveState> MoveStateQueue = new Queue<MoveState>();
|
public readonly Queue<MoveState> MoveStateQueue = new Queue<MoveState>();
|
||||||
@@ -35,6 +36,7 @@ namespace NBF.Fishing2
|
|||||||
protected override void Destroy(CharacterControllerComponent self)
|
protected override void Destroy(CharacterControllerComponent self)
|
||||||
{
|
{
|
||||||
self.characterController = null;
|
self.characterController = null;
|
||||||
|
self.IsSelf = false;
|
||||||
var mapUnit = self.Parent as MapUnit;
|
var mapUnit = self.Parent as MapUnit;
|
||||||
if (mapUnit.IsSelf())
|
if (mapUnit.IsSelf())
|
||||||
{
|
{
|
||||||
@@ -47,7 +49,6 @@ namespace NBF.Fishing2
|
|||||||
inputComponent.OnPlayerValueCanceled -= self.OnPlayerValueCanceled;
|
inputComponent.OnPlayerValueCanceled -= self.OnPlayerValueCanceled;
|
||||||
inputComponent.OnPlayerValuePerformed -= self.OnPlayerValuePerformed;
|
inputComponent.OnPlayerValuePerformed -= self.OnPlayerValuePerformed;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,6 +66,7 @@ namespace NBF.Fishing2
|
|||||||
var mapUnit = self.Parent as MapUnit;
|
var mapUnit = self.Parent as MapUnit;
|
||||||
if (mapUnit.IsSelf())
|
if (mapUnit.IsSelf())
|
||||||
{
|
{
|
||||||
|
self.IsSelf = true;
|
||||||
var inputComponent = self.Scene.GetComponent<InputComponent>();
|
var inputComponent = self.Scene.GetComponent<InputComponent>();
|
||||||
inputComponent.OnPlayerPerformed += self.OnPlayerCanceled;
|
inputComponent.OnPlayerPerformed += self.OnPlayerCanceled;
|
||||||
inputComponent.OnPlayerPerformed += self.OnPlayerPerformed;
|
inputComponent.OnPlayerPerformed += self.OnPlayerPerformed;
|
||||||
@@ -123,6 +125,8 @@ namespace NBF.Fishing2
|
|||||||
|
|
||||||
private void OnPlayerValuePerformed(InputAction.CallbackContext context)
|
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;
|
var name = context.action.name;
|
||||||
if (name == InputDef.Player.Move)
|
if (name == InputDef.Player.Move)
|
||||||
{
|
{
|
||||||
@@ -150,8 +154,10 @@ namespace NBF.Fishing2
|
|||||||
if (!isStop)
|
if (!isStop)
|
||||||
{
|
{
|
||||||
// 获取角色当前的右向和前向(在水平面上)
|
// 获取角色当前的右向和前向(在水平面上)
|
||||||
Vector3 characterRight = Vector3.ProjectOnPlane(characterController.transform.right, Vector3.up).normalized;
|
Vector3 characterRight =
|
||||||
Vector3 characterForward = Vector3.ProjectOnPlane(characterController.transform.forward, Vector3.up).normalized;
|
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;
|
movementDirection = characterForward * movementInput.y + characterRight * movementInput.x;
|
||||||
@@ -280,12 +286,6 @@ namespace NBF.Fishing2
|
|||||||
targetPosition += movement;
|
targetPosition += movement;
|
||||||
characterController.Move(movement);
|
characterController.Move(movement);
|
||||||
|
|
||||||
// 如果移动方向不为零,可以自动更新朝向
|
|
||||||
if (currentMoveState.moveDirection != Vector3.zero)
|
|
||||||
{
|
|
||||||
// networkFacingDirection = currentMoveState.moveDirection;
|
|
||||||
targetRotation = Quaternion.LookRotation(currentMoveState.moveDirection);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加位置和旋转插值方法
|
// 添加位置和旋转插值方法
|
||||||
@@ -346,6 +346,7 @@ namespace NBF.Fishing2
|
|||||||
// 检查朝向同步
|
// 检查朝向同步
|
||||||
private void CheckRotationSync()
|
private void CheckRotationSync()
|
||||||
{
|
{
|
||||||
|
if(!IsSelf) return;
|
||||||
rotationSyncTimer += Time.deltaTime;
|
rotationSyncTimer += Time.deltaTime;
|
||||||
if (rotationSyncTimer >= rotationSyncInterval)
|
if (rotationSyncTimer >= rotationSyncInterval)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using NBC;
|
using NBC;
|
||||||
using NBC.Entitas;
|
using NBC.Entitas;
|
||||||
using NBC.Entitas.Interface;
|
using NBC.Entitas.Interface;
|
||||||
|
using RootMotion.FinalIK;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace NBF.Fishing2
|
namespace NBF.Fishing2
|
||||||
@@ -15,6 +16,7 @@ namespace NBF.Fishing2
|
|||||||
public Transform Transform { get; set; }
|
public Transform Transform { get; set; }
|
||||||
|
|
||||||
public Animator Animator { get; set; }
|
public Animator Animator { get; set; }
|
||||||
|
public LookAtIK LookAtIK { get; set; }
|
||||||
|
|
||||||
public async FTask InitUnityObject()
|
public async FTask InitUnityObject()
|
||||||
{
|
{
|
||||||
@@ -22,7 +24,9 @@ namespace NBF.Fishing2
|
|||||||
GameObject = gameObject;
|
GameObject = gameObject;
|
||||||
Transform = gameObject.transform;
|
Transform = gameObject.transform;
|
||||||
Animator = gameObject.GetComponent<Animator>();
|
Animator = gameObject.GetComponent<Animator>();
|
||||||
|
LookAtIK = gameObject.GetComponent<LookAtIK>();
|
||||||
Parent.AddComponent<CharacterControllerComponent>();
|
Parent.AddComponent<CharacterControllerComponent>();
|
||||||
|
LookAtIK.enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user