How to make an area2D detect the collision of mouse controlled sprite?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl


Got my player/sprite on minimap movin freely, but my area2D/collisionshape2D don’t detect their collision. When a key moved sprite/player is detected.
How to make it for mouse controlled please?

I’m not sure I undestrood, can you be more specific?

DodoIta | 2018-04-04 13:05

Well, I’ve got a mini map, with a Player/sprite movin on it, triggerin scenes changes with area2D collisions. But that works only when my player/sprite is moved through the keyboard. When movin with the mouse, as I intend it to be, the area2Ds don’t detect my sprite/player anymore.

Here’s my code for mouse movement:

extends Node2D
var terrain_speed = 1.0
var speed = 3*50 # triple speed
var first_input = false

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
            var map_pos = $Map/Sprite.global_position
            var mouse_pos = get_global_mouse_position()
            var delta_position = mouse_pos - $Player.global_position # mouse click relative to player sprite
            var distance = map_pos.distance_to(mouse_pos)
            var duration = distance * terrain_speed/speed
            if first_input == false:
                first_input = true
            $Tween.interpolate_property($Map/Sprite, "global_position", map_pos, map_pos - delta_position, duration,
			 Tween.TRANS_LINEAR, Tween.EASE_IN)

And here’s the scene, to give you an idea:
Imgur: The magic of the Internet

Syl | 2018-04-04 13:30

:bust_in_silhouette: Reply From: DodoIta

I think the problem is that when you use the Tween, you type

$Tween.interpolate_property($Map/Sprite, …)

instead of just $Map, so basically you’re just moving the Sprite, which is not a Physics body, thus cannot be detected by an Area2D.
Try making collisions visible in the Debug option to see if I’m right.

When just $Map, on the very first click, my player is ‘teleported’ somewhere in the godot gray non-zone. Don’t know where…
I changed the player sprite as a child of its kinematicbody, wich is more correct, but still unfonctional. Tried to make the area2D ‘changing scene’ as a child of the map, so that it moves with it, but that doesn’t change anything.
Where’s the debug option to make collisions visible please?

Syl | 2018-04-05 14:14

It should be on top of the editor, Debug->Make collisions visible or something like that

DodoIta | 2018-04-05 14:38

Yes, that works, the area2D is movin away from my player… How to pin it then?

Syl | 2018-04-05 15:31

Ok, solved, had to put area2D in the sprite of the map.
Thxs for you! :slight_smile:

Syl | 2018-04-05 20:58

You’re welcome!

DodoIta | 2018-04-05 21:12