【发布时间】:2020-12-31 20:14:05
【问题描述】:
我正在开发一个 2D 点按式冒险游戏,但遇到了 2D 对撞机重叠问题。
这是游戏的一个示例场景:
发生了什么:
- 红色物体:玩家角色。具有禁用运动学的 RigidBody2D 和主要运动代码。
- 绿色背景:是角色移动的环境。使用 Box Collider 2D 并具有 OnGroundClick() 的事件触发器。
- Purple Object:是要检测的可交互项目。还有一个 Box Collider 2D 和 OnInteractableClick() 的事件触发器。
- 主摄像头有一个物理 2d 光线投射器。
这是播放器的代码:
public NavMeshAgent2D agent;
public float turnSmoothing = 15f;
public float speedDampTime = 0.1f;
public float slowingSpeed = 0.175f;
public float turnSpeedThreshold = 0.5f;
public float inputHoldDelay = 0.5f;
public Rigidbody2D body;
public bool freezeRotation;
private WaitForSeconds inputHoldWait;
private Vector3 destinationPosition;
private Interactables currentInteractable;
private bool handleInput = true;
private const float navMeshSampleDistance = 4f;
private const float stopDistanceProportion = 0.1f;
public void OnGroundClick(BaseEventData data)
{
Debug.Log("OnGroundClick");
if (!handleInput)
{
return;
}
currentInteractable = null;
PointerEventData pData = (PointerEventData)data;
NavMeshHit2D hit;
if (NavMesh2D.SamplePosition(pData.pointerCurrentRaycast.worldPosition,
out hit, navMeshSampleDistance, NavMesh2D.AllAreas))
{
destinationPosition = hit.position;
}
else
{
destinationPosition = pData.pointerCurrentRaycast.worldPosition;
}
agent.SetDestination(destinationPosition);
agent.isStopped = false; //agent.Resume();
}
public void OnInteractableClick(Interactables interactable)
{
Debug.Log("OnInteractableClick");
if (!handleInput)
{
return;
}
currentInteractable = interactable;
destinationPosition = currentInteractable.interactionLocation.position;
agent.SetDestination(destinationPosition);
agent.isStopped = false;
}
注意:NavMesh2D 是用于为 2D 环境创建导航网格的第三方资源
我已尝试将每个对撞机上的 z 位置更改为无效。
如何分别检测两个对撞机?
【问题讨论】:
标签: unity3d game-physics