#Handling clicking on game objects

1 messages · Page 1 of 1 (latest)

violet sigil
#

Hello everyone, can I just get a santity check. I'm building a simple puzzle game where game objects (meshes) are getting clicked. I currently have this working fine by including a empty game object in the scene that has a script attached to it. This script, on update, has the following code

     private void Update()
     {
         if (Mouse.current.leftButton.wasPressedThisFrame)
         {
             if (loadingCubesCamera == null) return;

             Ray ray = loadingCubesCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
             RaycastHit hit;

             if (Physics.Raycast(ray, out hit))
             {
                 InterferenceClickCube cubeClick = hit.collider.GetComponent<InterferenceClickCube>();
                 if (cubeClick != null)
                 {
                     cubeClick.OnClicked();
                 }
                 else
                 {
                     InterferenceClickFile fileClick = hit.collider.GetComponent<InterferenceClickFile>();
                     if (fileClick != null)
                     {
                         fileClick.OnClicked();
                     }
                 }
             }
         }
     }

Each game object that is clickable has a script attached to it (InterferenceClickCube, InterferenceClickFile) in the above example. I'm worried that as I add more clickable item types on the scene this approach is going to get messy. I'm not aware of any other approaches to doing this. Are there any alternatives? I'm open to any suggestions/comments and I'm happy to research if you could provide search terms to use.

acoustic crypt
#

consider using the built-in IPointerXHandlers with raycasters

violet sigil
#

@acoustic crypt : Thanks, I never heard of that prior. I will explore.

#

@acoustic crypt : Sorry if I missed something but based on my initial research that seems to be part of the old event system and it not part of Unity 6.2

acoustic crypt
#

what research is saying that?

wanton cairn
violet sigil
#

@wanton cairn I think that is what I am doing in the code I provided correct?

#

@acoustic crypt The link you provided is for UI. I'm not working with the UI, I'm working with 3d mesh objects. Is this still able to work for my use case?

acoustic crypt
#

if you have meshcolliders you can use a physicsraycaster on the camera, iirc

violet sigil
#

Ok. I will explore. Thanks for the explanation.

toxic harness
#

You can use monobehaviour method OnMouseDown()

glad fable
toxic harness
#

I using it currently in my projects it is working fine, I had mixed Input but when I publish for android/ios I chose the old input system because I was forced to chose the one.

#

I love the old mono mouse events. Sad if they are not working with new input system anymore.

glad fable
#

I say they are worse because the IPointer interface events from EventSystem do the same, provides more information AND works with UI so you dont need a work around to ignore input.

toxic harness
#

IPointer interfaces worked with UI but the monobehaviour methods worked with colliders thats why I used both methods

acoustic crypt
#

IPointer stuff can also work with colliders