Click on collision gameobject (SOLVED)

Hello,

I have a gameobject with a sprite and collisionobject with a sphere shape. Is there a way to detect if my mouse clicks the collisionobject?

Thanks

You need to have a smaller circle collision object which moves with your mouse cursor x,y then you can check collision based on that when you detect mouse input.

Or use two 2d raycasts in an X at your cursor to check against the other collision shapes.

1 Like

Raycast or a small collision object is the best solution. There’s a reusable cursor component with click and drag support here: https://github.com/britzl/defold-input

2 Likes

Im now using the dependency which works perfect, on desktop I can animate my gameobject in one click but on my device I have to tap two times on my gameobject before it animates. This only happens if I touch the gameobject for the first time after that I don’t have to touch two time before it gets animated.

Also, if I click on my gameobject it animates (as it should), but when I click outside my gameobject it also animates, but also just for the first time. This only happens on device.

Hi. I tested on device and found a few issues that I have now fixed in a new release:

Please give the new version a try and let me know if it works better!

1 Like

It works now on device where I only have to click once, but my gameobject still registers a touch if I touch outside the gamobject’s collider shape on device only.

This is what I have:

cursor controller script:

function init(self)
   if not go.get("#cursor", "acquire_input_focus") then
      self.forward_input = true
      msg.post(".", "acquire_input_focus")
   end
end

function on_message(self, message_id, message, sender)
    if message_id == hash("pressed") then
        if message.group == hash("planet") then
            msg.post(message.id, "input_planet")
        end
    end
end

function on_input(self, action_id, action)
    if self.forward_input then
        msg.post("#cursor", "input", { action_id = action_id, action = action })
    end
end

planet script:

function on_message(self, message_id, message, sender)
    if message_id == hash("input_planet") then
        go.animate(".", "scale", go.PLAYBACK_ONCE_PINGPONG, 0.7, go.EASING_INOUTCIRC, 0.15)
    end
end
1 Like

Can you share the project with me (bjorn.ritzl@king.com) so that I can take a look?

1 Like

I’ve sent the project.

1 Like

Thank you for sharing it! I’ve released a new version of the cursor script that should solve the problem you had: https://github.com/britzl/defold-input/releases/tag/1.5.3

3 Likes

Thank you! Everything is now working perfectly.:smiley:

1 Like