21 lines
632 B
C#
21 lines
632 B
C#
using UnityEngine;
|
|
|
|
namespace Utils
|
|
{
|
|
public static class RotateTool
|
|
{
|
|
// 旋转对象到指定方向
|
|
|
|
public static void RotateTransformToDirection(Transform transform, Vector3 targetDirection)
|
|
{
|
|
// 确保目标方向不是零向量
|
|
if (targetDirection == Vector3.zero)
|
|
return;
|
|
|
|
// 计算当前向上方向与目标方向之间的角度
|
|
var angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
|
|
// 应用旋转
|
|
transform.rotation = Quaternion.Euler(0f, 0f, angle);
|
|
}
|
|
}
|
|
} |