Only the Player
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 4 of 27.
An area notices every body that comes in. Often only one should count, like a door that opens only for the hero.
Use if to check who it is:
func _on_body_entered(body):
if body.name == "Player":
print("Hello, player")== asks whether two values are equal. The lines under the if, one tab further in, run only when they are. Names must match exactly. "player" with a small p is a different name.
Here the rover rolls over a pressure plate first, then you walk onto it.
Challenge
EasyThe plate turns mint for any body. Make it turn mint only for the Player. When the rover rolls over it, the plate should stay as it was.
Try it yourself
extends Area2D
func _ready():
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
modulate = Color(0.49, 0.95, 0.75)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
Practice on your own: Godot playground