1
This commit is contained in:
@@ -9,6 +9,9 @@ public class LinerMgr : MonoSingleton<LinerMgr>
|
||||
public float height = 0.5f;
|
||||
public float arrowLength = 0.5f;
|
||||
public float speed = 1f;
|
||||
public float groundHeight = 0.25f; // 地面导航时的高度
|
||||
public float startDistance = 2f; // 箭头起始距离(相机前方)
|
||||
public float backStartDistance = 5f; // 目标在后方时,箭头起始距离(更远)
|
||||
|
||||
public GameObject arrowPrefab;
|
||||
private Vector3 target;
|
||||
@@ -50,9 +53,58 @@ public class LinerMgr : MonoSingleton<LinerMgr>
|
||||
|
||||
void UpdateArrow()
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, target);
|
||||
float radius = height / 2f + distance * distance / (8f * height);
|
||||
float diff = radius - height;
|
||||
Vector3 actualTarget = target;
|
||||
|
||||
// 统一使用地面导航模式
|
||||
Camera mainCamera = Camera.main;
|
||||
if (mainCamera == null)
|
||||
{
|
||||
mainCamera = FindObjectOfType<Camera>();
|
||||
}
|
||||
|
||||
if (mainCamera != null && m_target != null)
|
||||
{
|
||||
actualTarget = m_target.position;
|
||||
|
||||
// 计算目标相对于相机的方向
|
||||
Vector3 cameraPos = mainCamera.transform.position;
|
||||
Vector3 cameraForward = mainCamera.transform.forward;
|
||||
Vector3 toTarget = actualTarget - cameraPos;
|
||||
float distanceToTarget = toTarget.magnitude;
|
||||
Vector3 toTargetDir = toTarget.normalized;
|
||||
|
||||
// 判断目标是否在相机后方(点积小于0表示在后方)
|
||||
float dotProduct = Vector3.Dot(cameraForward, toTargetDir);
|
||||
bool isBehind = dotProduct < 0f;
|
||||
|
||||
// 计算箭头起始位置 - 始终在相机前方,这样玩家才能看到
|
||||
Vector3 startPos;
|
||||
if (isBehind)
|
||||
{
|
||||
// 目标在后方:箭头起始点在相机前方,但箭头会向后指
|
||||
// 使用更远的距离,让箭头更明显
|
||||
startPos = cameraPos + cameraForward * backStartDistance;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 目标在前方或侧面:从相机前方开始,稍微偏向目标方向
|
||||
Vector3 startDir = Vector3.Slerp(cameraForward, toTargetDir, 0.3f).normalized;
|
||||
startPos = cameraPos + startDir * startDistance;
|
||||
}
|
||||
|
||||
// 设置高度为接近地面的高度(目标高度 + 地面高度偏移)
|
||||
startPos.y = actualTarget.y + groundHeight;
|
||||
transform.position = startPos;
|
||||
}
|
||||
else if (m_target != null)
|
||||
{
|
||||
// 没有相机,使用目标位置
|
||||
actualTarget = m_target.position;
|
||||
}
|
||||
|
||||
float distance = Vector3.Distance(transform.position, actualTarget);
|
||||
float radius = groundHeight / 2f + distance * distance / (8f * groundHeight);
|
||||
float diff = radius - groundHeight;
|
||||
float angle = 2f * Mathf.Acos(diff / radius);
|
||||
float length = angle * radius;
|
||||
float segmentAngle = arrowLength / radius * Mathf.Rad2Deg;
|
||||
@@ -75,9 +127,13 @@ public class LinerMgr : MonoSingleton<LinerMgr>
|
||||
arrowList[i].localRotation = Quaternion.FromToRotation(Vector3.up, pos - center);
|
||||
|
||||
}
|
||||
transform.LookAt(m_target);
|
||||
|
||||
if (m_target != null)
|
||||
{
|
||||
transform.LookAt(actualTarget);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ArrowInit(int listCount)
|
||||
{
|
||||
while (arrowList.Count < listCount)
|
||||
|
||||
Reference in New Issue
Block a user