This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello all!

I'm having a bit problem while dealing with RigidBody and collision.

What I'm doing is instancing some nodes while clicking a button, a new node is instanced in a different position.

Those nodes need to be able to move by drag and drop on mouses, and initially, I did with Area2d nodes and it was working fine, however, I need it to handle collisions and I used the same principle for moving it.

The problem is that when I move using a rigid body, the movement appears to be limited to the rigid body area itself.

Trying to change the rigid body to a character also has the same effect, however, it resets to the initial position.

Note that in the background, there is a Are2d, however, to avoid issues, I also changed the layer/mask, however, no luck.

Here is the code I'm using to move them.

  var id
var MouseIn = false
signal MouseIn
signal MouseOut
func _ready():
    set_process(true)
    pass
func add_peca(id):
    print (id)
    pass

func _process(delta):
    if(MouseIn == true && Input.is_action_pressed("click")):
        set_position(get_viewport().get_mouse_position())
    pass
func get_MouseIn():
    return MouseIn
func _on_mouse_entered():
    MouseIn = true
    emit_signal("MouseIn")
    pass 
func _on_mouse_exited():
    MouseIn = false
    emit_signal("MouseOut")
    pass 

Do you know what i'm doing wrong? It's the movement script itself or there is any property i'm not aware that cause it?

in Engine by (18 points)

1 Answer

+1 vote
Best answer

A rigid body cannot be moved by changing its position, because the physics engine has control of it. If you need to move it manually, you must either directly alter its physics state in _integrate_forces() or set it to MODE_STATIC so that it's removed from physics processing. The latter is the better solution for drag-and-drop.

Here's an example of this that I put together after getting similar questions often:
http://kidscancode.org/godot_recipes/physics/rigidbody_drag_drop/

Aside from that, there are some other things in this code that are wrong or are going to cause you trouble:

  • MouseIn is used as the name of both a signal and a variable (although it doesn't look like that signal is used)
  • pass is extraneous. It does nothing.
  • set_process(true) has not been required since Godot 2.x
  • Rigid bodies (all CollisionObject2D nodes) can detect input events, so it's not necessary to separately detect mouse in and click events. See inputevent signal.

Recommended reading:

by (22,191 points)
selected by

Hello kidsscancode,

thanks a lot for the insight, this helped a lot!

didn't know of the set_process(true) and pass thing, and you code helped me a lot to understand.

Just one thing to leave registered, the MouseIn variable actually have a diferent name on my code, i really didn't noticed i used the same name from the signal when i posted here.

By the way, i love your tutorial videos!

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 Frequently asked questions and 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 [email protected] with your username.