0 votes

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)
in Engine by (83 points)

1 Answer

0 votes
Best answer

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.
by (1,330 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.