首次提交
This commit is contained in:
59
Assets/Scripts/Demo/ArmTest.cs
Normal file
59
Assets/Scripts/Demo/ArmTest.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using NBF;
|
||||
using UnityEngine;
|
||||
|
||||
public class ArmTest : MonoBehaviour
|
||||
{
|
||||
public Vector3 target = new Vector3(0, 0, 0);
|
||||
|
||||
public Transform targetTransform;
|
||||
// public float Value;
|
||||
|
||||
private Quaternion initialCameraRotation;
|
||||
private Quaternion initialLocalRotation;
|
||||
private Quaternion initialRelativeRotation;
|
||||
|
||||
|
||||
[SerializeField] private float smoothSpeed = 5f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// target = transform.localEulerAngles;
|
||||
|
||||
// 记录初始状态
|
||||
initialCameraRotation = BaseCamera.Main.transform.rotation;
|
||||
initialLocalRotation = transform.localRotation;
|
||||
|
||||
// 计算物体相对于相机的旋转
|
||||
initialRelativeRotation = Quaternion.Inverse(initialCameraRotation) * transform.rotation;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private Quaternion lastCameraRotation;
|
||||
[SerializeField] private float angleThreshold = 0.1f; // 角度阈值(度)
|
||||
private bool needsUpdate;
|
||||
|
||||
// private void LateUpdate()
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!targetTransform) return;
|
||||
return;
|
||||
// Debug.LogError(targetTransform.rotation);
|
||||
|
||||
|
||||
// 计算物体应有的世界旋转
|
||||
Quaternion targetWorldRotation = BaseCamera.Main.transform.rotation * initialRelativeRotation;
|
||||
|
||||
// 转换为本地旋转(如果是子物体)
|
||||
transform.localRotation = Quaternion.Inverse(transform.parent.rotation) * targetWorldRotation;
|
||||
|
||||
|
||||
// // 使用Lerp平滑过渡
|
||||
// transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(target.x, target.y, target.z), smoothSpeed * Time.deltaTime);
|
||||
// // targetTransform.localRotation = Quaternion.Euler(target.x, target.y, target.z);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user