Godot Version
v4.2.1 stable mono official
Question
I am trying to separate the collision of my character’s movement and hitbox but it seems like I can only put a KinematicBody2D in one collision layer. is there a way to have two collision boxes in one Node2D. For context, I want to select my unit through its hitbox and let it collide with other objects through its movement collision shape. I need help to implement this…
You should be able to use the Area2D you currently have in the Node to implement a solution for selecting a unit with your mouse, if I’m correct in understanding that that’s what you want.
You can use signals from Area2D (mouse_entered, mouse_exited) to, for example, switch a bool variable, then add that variable as a condition to process your input event. Like, if you wanted to store your CharacterBody2D as a target:
(knight.gd)
var target
var valid_target
func on_area_2d_mouse_entered():
var valid_target = true
func on_area_2d_mouse_exited():
var valid_target = false
func _input(event):
if Input.is_action_pressed("left_mouse") and valid_target:
target = self
This is just an example and it depends on what you want to happen when you select it, of course.
Thank you for replying! I appreciate this approach you’ve made. However, I use a RTS approach in selecting my units by drawing a rect and colliding with whatever object is in inside it. Here’s my code on my selection box:
I was wondering if there was a way to directly collide with the area2D collision shape instead rather than the character’s movement collision in this case…
I see that you commented out the collision_mask part, so you may have already tried this. Have you checked the collision layers in your nodes? If the Area2D is in Layer 2 and the rect is Mask 2, did that solution not work already?
Yes. I have tried setting the mask layer to 2 where the area2d (hitbox) is on layer 2 but it doesn’t collide with it. It only collides with the character which is on layer 1 when the mask of the query is on layer 1 as well.