This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

I am asking for a code which is responsible for attaching the object to the mouse.

dzosekdzosek Posts: 37Member

I need that kind of reaction for my object: if I click on my object I want him to cling to my cursor and then unhook only when I click the mouse button. I'm asking for a code which is responsible for attaching the object to the cursor.

Best Answer

  • beanpuffsbeanpuffs Posts: 7
    edited February 2020 Accepted Answer

    This should be simple enough (Assuming 2D)

    The most involved step is to set up the click detection. The easiest way to do this is to set up a Physics Body (KinematicBody2D, StaticBody2D, RigidBody2D, etc) and toggle the "pickable" option in its collision settings. This will allow it to send a signal when the mouse is within the Body's collision and does any action.
    The script will be attached to this node, and also for this example I will use a KinematicBody2D.

    Set up this Node Path:

    You will only need a script on the KinematicBody2D for now, the script on my sprite here is just so that it animates.

    Make sure your Collision2D has a proper Shape set up:

    Check this box on your Physics Body / KinematicBody2D:

    And then go into your KinematicBody's signals and double click the "input_event" signal. Connect it to your KinematicBody2D. It won't let you do this if your KinematicBody2D does not have a script.

    After you connect the signal it will add a function into your script, this is as we want it.

    In your object's script, add a boolean var called something like "mouse_attach" or however makes sense to you.
    Any behavior you don't want to allow while the object is being moved with the mouse simply put inside an if statement that doesn't allow it to work when your mouse_attach boolean is true.

    While mouse_attach is true however, have the object's transform.origin be changed to match the mouse position in the window.
    This step has an added complexity if you use a 2D camera because the mouse's coordinates are mapped to the viewport window and not the game world's coordinates.
    If you plan to have everything be in the window at all times and never use a 2D camera to view only select parts of your environment, then you can simply move the object with the window's viewport coordinates, otherwise you will add the Camera's offset as well.

    Finally, add an event that toggles the mouse_attach variable when the object has the mouse released inside it. (ie it is clicked.)
    Alternatively if you want it to be draggable instead of a click to grab and click to release you can set mouse_attach to true on pressed and then to false on released.

    After all that it should look something like this:

    Keep in mind that Control nodes can make the mouse events not work. If you have a large GUI node over the whole screen and want to be able to click through it, make sure it's Mouse property is set to Ignore.

Answers

  • beanpuffsbeanpuffs Posts: 7Member
    edited February 2020 Accepted Answer

    This should be simple enough (Assuming 2D)

    The most involved step is to set up the click detection. The easiest way to do this is to set up a Physics Body (KinematicBody2D, StaticBody2D, RigidBody2D, etc) and toggle the "pickable" option in its collision settings. This will allow it to send a signal when the mouse is within the Body's collision and does any action.
    The script will be attached to this node, and also for this example I will use a KinematicBody2D.

    Set up this Node Path:

    You will only need a script on the KinematicBody2D for now, the script on my sprite here is just so that it animates.

    Make sure your Collision2D has a proper Shape set up:

    Check this box on your Physics Body / KinematicBody2D:

    And then go into your KinematicBody's signals and double click the "input_event" signal. Connect it to your KinematicBody2D. It won't let you do this if your KinematicBody2D does not have a script.

    After you connect the signal it will add a function into your script, this is as we want it.

    In your object's script, add a boolean var called something like "mouse_attach" or however makes sense to you.
    Any behavior you don't want to allow while the object is being moved with the mouse simply put inside an if statement that doesn't allow it to work when your mouse_attach boolean is true.

    While mouse_attach is true however, have the object's transform.origin be changed to match the mouse position in the window.
    This step has an added complexity if you use a 2D camera because the mouse's coordinates are mapped to the viewport window and not the game world's coordinates.
    If you plan to have everything be in the window at all times and never use a 2D camera to view only select parts of your environment, then you can simply move the object with the window's viewport coordinates, otherwise you will add the Camera's offset as well.

    Finally, add an event that toggles the mouse_attach variable when the object has the mouse released inside it. (ie it is clicked.)
    Alternatively if you want it to be draggable instead of a click to grab and click to release you can set mouse_attach to true on pressed and then to false on released.

    After all that it should look something like this:

    Keep in mind that Control nodes can make the mouse events not work. If you have a large GUI node over the whole screen and want to be able to click through it, make sure it's Mouse property is set to Ignore.

Sign In or Register to comment.