上下视角控制

This commit is contained in:
2026-01-18 01:05:56 +08:00
parent a46436ae45
commit 7b2a93d673
9 changed files with 128 additions and 5 deletions

View File

@@ -0,0 +1,49 @@
using UnityEngine;
namespace NBF
{
public static class GameUtils
{
public static float GetVerticalAngle(Transform character, Transform target)
{
// 计算从角色指向目标的向量
Vector3 direction = target.position - character.position;
// 计算水平距离XZ平面
float horizontalDistance = new Vector3(direction.x, 0, direction.z).magnitude;
// 计算垂直高度差Y轴
float verticalDistance = direction.y;
// 使用反正切计算垂直角度
// Atan2(垂直距离, 水平距离)
float angle = Mathf.Atan2(verticalDistance, horizontalDistance) * Mathf.Rad2Deg;
return angle; // 正值为上方,负值为下方
}
public static float GetHorizontalAngle(Transform obj1, Transform obj2)
{
// 获取两个物体在水平面上的位置向量
Vector3 pos1 = obj1.position;
Vector3 pos2 = obj2.position;
// 创建水平方向向量忽略Y轴
Vector3 direction = new Vector3(
pos2.x - pos1.x,
0, // Y轴设为0只在XZ平面计算
pos2.z - pos1.z
);
// 计算相对于世界坐标系前向向量的角度
float angle = Vector3.SignedAngle(
Vector3.forward, // 参考方向(世界坐标系前向)
direction.normalized,
Vector3.up // 旋转轴Y轴
);
// 返回角度范围:-180° 到 180°
return angle;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a1e142c7ecd747eaa6971907ecae2c44
timeCreated: 1768661644

View File

@@ -40,6 +40,7 @@ namespace NBF
FirstPerson = gameObject.GetComponent<FirstPersonCharacter>();
Data = FPlayerData.Instance;
transform.localPosition = new Vector3(484, 1, 422);
// Data.NeedChangeRightArmAngle = true;
}
private void Start()
@@ -53,6 +54,8 @@ namespace NBF
{
UpdateMove();
Fsm?.Update();
Data.EyeAngle = GameUtils.GetVerticalAngle(transform, FppLook);
}
private void OnDestroy()

View File

@@ -28,6 +28,7 @@ namespace NBF
public enum PlayerState : uint
{
None = 0,
/// <summary>
/// 闲置等待中
/// </summary>
@@ -74,6 +75,11 @@ namespace NBF
public Vector2 MoveInput;
/// <summary>
///
/// </summary>
public float EyeAngle;
public PlayerState PreviousState => _previousPlayerState;

View File

@@ -1,15 +1,29 @@
using RootMotion.FinalIK;
using System;
using RootMotion.FinalIK;
using UnityEngine;
namespace NBF
{
public class PlayerArm : MonoBehaviour
{
public bool FixLowerArm;
public bool IsLeft;
public LimbIK IK;
public Transform LowerArm;
public Transform RodContainer;
public FingerRig FingerRig;
[HideInInspector] public float interactionTargetWeight;
private const int MaxFixEyeAngle = 15;
public void Awake()
{
}
private void LateUpdate()
{
}
}
}

View File

@@ -0,0 +1,25 @@
using UnityEngine;
namespace NBF
{
public class PlayerChest : MonoBehaviour
{
private const int MaxFixEyeAngle = 15;
private const int MinFixEyeAngle = -10;
private void LateUpdate()
{
FixArmAngle();
}
private void FixArmAngle()
{
var angle = FPlayerData.Instance.EyeAngle;
if (angle > MaxFixEyeAngle) angle = MaxFixEyeAngle;
else if (angle < MinFixEyeAngle) angle = MinFixEyeAngle;
var val = transform.localEulerAngles;
transform.localEulerAngles = new Vector3(val.x, val.y, val.z - angle);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e152a74e74b54d17ace5403a1570e12a
timeCreated: 1768668096