I'm developing a 2D point and click adventure and am having issues with the 2D colliders over lapping.
Here is a sample scene of the game:
What's going on:
- Red Object: the player character. Has a RigidBody2D with kinematic disabled and the main movement code.
- Green Background: is the environment of which the character moves. Uses a Box Collider 2D and has an event trigger for OnGroundClick().
- Purple Object: is the interactable item to be detected. Also has a Box Collider 2D and an event trigger of OnInteractableClick().
- The main camera has a physics 2d ray caster.
This is the code for the player:
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;
}
Note: NavMesh2D is a third party asset for creating nav meshes for 2D environments
I have tried changed the z position on each collider to no effect.
How can I detect both colliders individually?