I'm struggling with picking the GameObject that I click on. This is my code:
Code:
public GameObject GetGameObjectAtScreenPos(Vector2 screenPos)
{
GameObject gameObjHit = null;
ShapeInfo shape = RigidBody.PickShapeGlobal(MainCamera.GetSpaceCoord(screenPos).Xy);
if (shape != null)
{
gameObjHit = shape.Parent.GameObj;
}
return gameObjHit;
}
That works perfectly when the GameObjects are separate from each other. It even works when I scale up or rotate the GameObjects. The RigidBodies seem to scale and rotate accordingly.
But when a GameObject overlaps antoher one, that's when I'm having issues. The game object returned is not always the one on top. Sometimes the game object in the background (on the bottom) is picked. And that's not what I want.
I have tried different SpriteRenderer.Offsets, and different z-values, but that don't make a difference it seems.
So how do I solve this? Isn't the RigidBody.PickShapeGlobal-function supposed to work with GameObjects in different planes (different z-values)?
I have read
http://www.adamslair.net/forum/viewtopic.php?f=4&t=1007 and
http://www.adamslair.net/forum/viewtopic.php?f=4&t=307&start=0, but they don't answer my problems.