0 votes

i use KinematicBody2D
how to get mask id
example when i collide with mask 1(player) = queue_free(), mask 3(stone) = stop move, mask 5(item) = change color

if i code like this

if move_and_collide(velocity*delta):

it will apply to all mask.. so how to make it apply to only one mask
enter image description here

in Engine by (272 points)
edited by

2 Answers

+1 vote
Best answer

I don't think you can get a node's collision layer or mask (Never mind, you can. See njamster's answer) but you could use groups to accomplish what you are trying to do.

var collision = move_and_collide(velocity * delta)
if collision:
    var collider = collision.collider

    if collider.is_in_group("Player"):

    if collider.is_in_group("Rock"):

    if collider.is_in_group("Item"):

If there is only one player then you could also use its name instead of using a group with only one node.

if collider.name == "Player":
by (156 points)
edited by

You can use MATCH instead of using many IF

thank you for your help

@Mk_: No, you cannot use match here as there is no common pattern.

+1 vote

You can do:


This will return true if the first bit of the layer/mask is set or false otherwise. To get the second bit instead, just pass 1 instead of 0 as an argument. And so on.

by (10,055 points)
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.