Is it possible to make the mouse cursor collide with 2D objects?

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

I am new to Godot so I don’t know a lot of stuff. I tried by making a fake cursor using a 2D kinematic body that follows the mouse cursor around. The problem is that it doesn’t collide with a 2D static body that has a 2D collisionshape. For some reason, the cursor can pass through the collision shapes. I tried changing the staticbody into a rigidbody and the cursor collided with it properly. The problem with this is that I can push the rigid body around. Is there a solution to this problem?

Can you provide your code and/or an example project? How do you change the fake cursors position? Have you changed the collision-layer or- mask of any of the nodes involved? A KinematicBody2D can collide with a StaticBody2D, so the problem is likely related to something else you did. But it’s impossible to tell what exactly that might be, given the amount of information you’ve provided so far.

njamster | 2020-04-21 16:38

This is attached to the kinematicbody2D:

extends KinematicBody2D
var mouseposition

func _process(delta):
mouseposition = get_global_mouse_position()
position = mouseposition

The fake cursor follows the mouse’s position.

NathanielFumar | 2020-04-22 04:21

:bust_in_silhouette: Reply From: njamster

A KinematicBody2D does not check for collisions if you set it’s position-property manually. Use move_and_collide instead:

extends KinematicBody2D

func _ready():

func physics_process(delta):
    move_and_collide(get_global_mouse_position() - global_position)