我正在开发2D点并单击冒险,并且在研磨过程中遇到了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位置更改为无效。
如何分别检测两个对撞机?
答案 0 :(得分:0)
我在背景的光线投射器中添加了一些细节。将其移至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();
}