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

My first goal is for players to not be able to walk through enemies. Second, I want the enemy's name and HP bar to appear when the enemy is clicked on. I can only achieve one goal but not both. I'm using Godot v3.0.6 & GDScript.

The relevant part of the tree~ Name (Node Type) - description:

Main (Node) - the root
-UI (CanvasLayer)
--OtherHP (TextureProgress) - the targeted enemy or ally's HP bar
-Enemy (Node) - all enemies will be children of this node
--BadCat (KinematicBody2D) - my test enemy
--Sprite (Sprite) - my placeholder sprite, static for now
---EnemyName (Label) - his name appears over his head
--CollisionPolygon2D (CollisionPolygon2D) - drawn over the sprite's feet

~ Alternative Enemy Tree ~

-Enemy (Node)
--BadCat (Area2D) - change kinematicbody2D to area2D + fix references in scripts
--Sprite (Sprite)
---EnemyName (Label)
--CollisionPolygon2D (CollisionPolygon2D)

In Main

func _ready():
$UI/OtherHP.hide()
$"Enemy/BadCat/Sprite/EnemyName".hide()

On BadCat, the input_event( Object viewport, Object event, int shape_idx) signal is connected to ../../UI :: _on_BadCat_input_event()

In UI,

func _on_BadCat_input_event(viewport, event, shape_idx):
print ("test")
if Input.is_action_pressed("click"):
    $OtherHP.show()
    $"../Enemy/BadCat/Sprite/EnemyName".show()

If I use the KinematicBody2D version, my player sprite cannot walk through the enemy. However, mousing over and clicking does nothing. The HP bar and name remain hidden.

kinematic results

If I use the Area2D version, mousing over prints lots of "test" and clicking causes the HP bar and name to show. However my player sprite phases through the enemy like it's a ghost.

area results

How can I have it all: corporeal and clickable enemies?

in Engine by (19 points)

2 Answers

+1 vote
Best answer

Select Your KinematicBody2D and check "Pickable" in the Inspector.

by (2,308 points)
selected by

That fixes it and is cleaner than my fix! Thanks a lot.

0 votes

I got a working version after 4 days. Figures that I would get it an hour after posting here.

~ New Enemy Tree ~

-Enemy (Node)
--BadCat (Area2D)
--Sprite
---EnemyName
--CollisionPolygon2D
--KinematicBody2D
---CollisionPolygon2D

I added a KinematicBody2D child to BadCat. The second CollisionPolygon2D is a duplicate & reparent of the first. Then boom, it works. Easy fix.

both area and kinematic results

If you know a better way, please share for me and other learners!

by (19 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 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.