Mouse input help

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noob_Maker
:warning: Old Version Published before Godot 3 was released.

I’m making a mechanic in my game where you can interact with certain objects by left clicking. I have the code without any errors for the object that I want to test it on but the problem is that for some weird reason I cannot actually left click the object.

I think the problem is that I didn’t setup the input for the mouse correctly but it might be something else.

Here’s the code for the object that I’m testing the interaction feature:


extends RigidBody2D
var pos = Vector2()
var offset = Vector2()
var Click =  Input.is_action_pressed("Interaction")
func _ready():
	set_gravity_scale(3)
	
func _fixed_process(delta):
	Click = Input.is_action_pressed("Interaction")
	if Click == true:
		set_global_pos(pos + offset)
:bust_in_silhouette: Reply From: YeOldeDM

Two Things:

  • You need to enable fixed process for _fixed_process(delta) to run, set_fixed_process(true) under _ready (I forget to do this a lot too!)
  • Once you do that, the script should then register your Click input. However, you have no way of checking the position of the mouse when you click, to see if it’s inside the shape of your rigidbody. The behavior I expect from your script would be for the rigidbody to teleport to the top-left corner of your screen any time you click on the screen.

Thanks! but how do you make it to follow the mouse when the left click is held down instead of teleporting?

Noob_Maker | 2017-06-10 14:47