You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity抓钩游戏:基于角度判断移除最后锚点的逻辑问题

Unity抓钩游戏锚点移除逻辑问题

我正在Unity开发一款抓钩风格的游戏,当绳索与物体碰撞时,会将锚点移动到碰撞位置,并把这些位置存入List<Vector2>中。

目前需要根据玩家与最后一个锚点、最后一个锚点与倒数第二个锚点之间的角度关系,判断是否移除最后一个锚点。

场景说明

  • 正常场景:A为初始锚点,B为新增锚点,绿色图标代表玩家,此时不需要移除锚点。
  • 需要移除的场景:当玩家与B的角度小于或等于B与A的角度时,需要移除锚点。
  • 失效场景:当前逻辑在部分场景下无法正确触发移除操作。

我难以确定正确的逻辑,担心自己把问题过度复杂化了。以下是当前仅在80%场景有效的逻辑代码:

Vector2 playerPosition = player.transform.position;
Vector2 lastAnchor = anchorPositions[anchorPositions.Count - 1];
Vector2 secondToLastAnchor = anchorPositions[anchorPositions.Count - 2];

Vector2 directionToLastAnchor = lastAnchor - playerPosition;
Vector2 directionToSecondToLastAnchor = secondToLastAnchor - lastAnchor;

bool anchorToLeftOfPlayer = directionToLastAnchor.x < 0;
bool anchorToRightOfPlayer = directionToLastAnchor.x > 0;

float angleToLastAnchor = Vector2.SignedAngle(playerPosition, lastAnchor);
float angleToSecondToLastAnchor = Vector2.SignedAngle(secondToLastAnchor, lastAnchor);

if (anchorToLeftOfPlayer) {
    if (angleToLastAnchor > angleToSecondToLastAnchor && directionToLastAnchor.x > directionToSecondToLastAnchor.x) {
        Debug.Break();
        DetachRopeAtIndex(anchorPositions.Count - 1);
    }
} else if (anchorToRightOfPlayer) {
    if (angleToLastAnchor < angleToSecondToLastAnchor && directionToLastAnchor.x < directionToSecondToLastAnchor.x) {
        Debug.Log("Detached because anchor on right of player");
        Debug.Break();
        DetachRopeAtIndex(anchorPositions.Count - 1);
    }
}

易懂版逻辑:如果锚点在玩家右侧,且角度B < 角度A,同时方向B < 方向A → 移除锚点

我不确定自己是否:

  • 正确计算了角度
  • 应用了正确的逻辑
  • 没有过度复杂化问题
  • 错误地将逻辑交由Unity处理

内容的提问来源于stack exchange,提问作者Cjmarkham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:42:08