Unity 2D 重叠碰撞器
Unity 2D Overlapping Colliders
我正在开发一个 2D 点和点击冒险,我在重叠时遇到 2D 碰撞器的问题。
这是游戏的示例场景:
发生了什么:
- 红色对象:玩家角色。具有禁用运动学和主要运动代码的 RigidBody2D。
- 绿色背景:是角色移动的环境。使用 Box Collider 2D 并具有 OnGroundClick() 的事件触发器。
- 紫色对象:是要检测的可交互项目。还有一个 Box Collider 2D 和一个 OnInteractableClick() 事件触发器。
- 主摄像头有物理二维光线投射器。
这是播放器的代码:
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 位置,但没有效果。
如何单独检测两个对撞机?
我为背景添加了一些细节到 raycaster。把它移到z轴的最后面,然后我修改了它的代码。
public void OnGroundClick(BaseEventData data)
{
Debug.Log("OnGroundClick");
if (!handleInput)
{
return;
}
currentInteractable = null;
PointerEventData pData = (PointerEventData)data;
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(pData.pointerCurrentRaycast.worldPosition);
worldPoint.z = Camera.main.transform.position.z;
Ray ray = new Ray(worldPoint, new Vector3(0, 0, 1));
RaycastHit2D hitInfo = Physics2D.GetRayIntersection(ray);
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();
}
我正在开发一个 2D 点和点击冒险,我在重叠时遇到 2D 碰撞器的问题。
这是游戏的示例场景:
发生了什么:
- 红色对象:玩家角色。具有禁用运动学和主要运动代码的 RigidBody2D。
- 绿色背景:是角色移动的环境。使用 Box Collider 2D 并具有 OnGroundClick() 的事件触发器。
- 紫色对象:是要检测的可交互项目。还有一个 Box Collider 2D 和一个 OnInteractableClick() 事件触发器。
- 主摄像头有物理二维光线投射器。
这是播放器的代码:
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 位置,但没有效果。
如何单独检测两个对撞机?
我为背景添加了一些细节到 raycaster。把它移到z轴的最后面,然后我修改了它的代码。
public void OnGroundClick(BaseEventData data)
{
Debug.Log("OnGroundClick");
if (!handleInput)
{
return;
}
currentInteractable = null;
PointerEventData pData = (PointerEventData)data;
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(pData.pointerCurrentRaycast.worldPosition);
worldPoint.z = Camera.main.transform.position.z;
Ray ray = new Ray(worldPoint, new Vector3(0, 0, 1));
RaycastHit2D hitInfo = Physics2D.GetRayIntersection(ray);
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();
}