0 votes

Hi,

I'm a new learner of Godot and Im trying to use body_entered signal to detect collision between a RigidBody2D object and a StaticBody2D obstacle, BUT it does not work and I don't know why. Please help me with this issue. Here is my code and I did not know which is wrong to let it work:

extends RigidBody2D

func _ready():
  set_process_input(true)
  connect("body_entered", self, "_on_character_body_entered")
  pass

func _input(event):
  if event.is_action_pressed("move"):
    move()
  pass

func move():
  set_linear_velocity(Vector2(50,get_linear_velocity().y))
  pass

func _on_character_body_entered():
  print("B")
  _bounce()
  pass

func _bounce():
  set_linear_velocity(Vector2(get_linear_velocity().x,50))
  pass  
asked Dec 13, 2019 in Engine by CrinstalMaiden (20 points)
edited Dec 13, 2019 by CrinstalMaiden

1 Answer

+1 vote
Best answer

In order for a rigid body to report collisions, you must enable "Contact Monitoring" and set "Contacts Reported" to a value greater than 0.

Some other comments:

You cannot move a rigid body by setting its linear velocity directly like that. You must apply forces or impulses, because a rigid body is controlled directly by the physics engine, not by you. See Physics Introduction: RigidBody2D and the RigidBody2D documentation for more information.

Also, it looks like you've been using old tutorial material for pre 3.0 versions of Godot. You don't need to set input processing, or use set()/get() on object properties.

Lastly, you don't need to put pass everywhere. It literally does nothing.

answered Dec 13, 2019 by kidscancode (16,476 points)
selected Dec 13, 2019 by CrinstalMaiden

Thank you,

It worked! I found the description of body_entered signal in the documentation as well. For the input process and pass, could you show me where to read more or any tutorial of the update 3.1?

Im Really appreciated for your comments :)

The transition from 2.1 to 3.0 happened some time ago. Basically any tutorial that's less than 2 years old is going to be fine. Older than that and you'll have to change a lot of things.

If you want to learn the engine with up-to-date information, the official docs are by far the best place to start: Documentation: Step by step.

pass has nothing to do with version. It's a command that literally means "do nothing". Putting it at the end of every one of your functions is doing nothing but taking up space. The only time you ever need to use pass is when you need a placeholder for a function body that you're planning to fill in later.

Thank you,

I watched some tutorial and I thought that pass is to end the function :P

Have a nice day!

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.