Adam's Lair Forum

game development and casual madness
It is currently 2018/06/18, 07:27

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2018/06/09, 15:03 
Newbie
Newbie

Joined: 2017/08/13, 16:10
Posts: 7
Role: Hobbyist
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.


Top
 Profile  
 
PostPosted: 2018/06/10, 14:53 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2049
Location: Germany
Role: Professional
Hey there,

picking operations using 2D physics are always locked to the Z-0 plane and have no concept of an object "behind" or "in front of", since this dimension doesn't exist there. You can, however, write some logic manually to find the topmost object at the position. Instead of PickShapeGlobal, use the multiple-object version PickShapesGlobal, and then use the result with the highest or lowest depth value, or offset.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2018/06/11, 07:51 
Newbie
Newbie

Joined: 2017/08/13, 16:10
Posts: 7
Role: Hobbyist
Thats awesome, thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group