How to select GO with touch and them move it with another? (SOLVED)

Hello!

I am very new to Defold and I need your help:

I have a gameobject(with planet sprite and collision object with sphere shape) and another gameobject(it should be a cursor). My plan is to make movement of planet that way:
1)User clicks on planet(selects it)
2)User clicks on place it should be moved to
3)Planet moves to that place

I have no problem with 2 and 3 steps(I read tutorials and so on), but I have difficultes in 1 step.
I am trying to make it with collisions, but I don’t understand how it works, I haven’t found any examples.

An example of clicking on game objects with touch/mouse input you can find here (Cursor and Gesture Demo):

Check out if something from this asset might be useful for you with its demo:

https://britzl.github.io/Defold-Input/

This might make things easier for you. If you would like to write it on your own - you will need to convert action screen coordinates to your world coordinates - it depends then on what camera you are using - is it a game with one screen or can you move around your view around the world (space in your case I guess :smiley: ) . If you are using Rendercam or Defold Orhtographic Camera for this - then they have conversion functions already :wink:

2 Likes

Thanks!

But I am afraid this is not the thing I actually need.

I have this script for cursor right now:

function init(self)
	msg.post(".", "acquire_input_focus") 
	
	self.collided_id = nil     -- id of gameobject which the cursor collided with
	self.dragged_pos = nil		-- The position of the dragged_id game object
	self.owned = false
	
end

function on_input(self, action_id, action)
	

	if action_id == hash("touch") then
		msg.post("#collisionobject", "enable")
		local position = vmath.vector3(action.x, action.y, 0)
		go.set_position(position, ".")
	
	end	

		
end

function on_message(self, message_id, message, sender)
	-- check for the message
	if message_id == hash("collision_response") then
		-- take action
		print("I collided with", message.other_id)
	end
end
--[[

Don’t care about unused variables. This script controls cursor, moves its GO when detects touch on screen. But, when I click on another GO with Collision Object, it doesn’t work!

Have I made any mistake?

1 Like

Sometimes a GO with a collision object doesn’t move because of the type of collision object (some of them are immovable). Try changing the type of collision object to see if that helps.

It doesn’t even collide. If it collided, it would print a message in console(as defined in script). But it doesn’t.

Both of objects are kinematic.

What are the groups/masks of your collision objects? That’s a potential pitfall.

Check this out:

2 Likes

Thanks!

But both of collision objects(cursor and planet) are DEFAULT group and mask.

I was able to solve this issue:

Problem was in location of planet. Sprite was on 60, 60, 0 and its collision object on 0,0,0)

1 Like

Great, that you found the cause! :blush::+1:

1 Like