Description
Just a quick one I wanted to cover, hopefully there’s still a few people out there who don’t know how to do this so my effort won’t be wasted :-)The best way…
My Notes
00:00 The code!
extends Sprite2D
func _input(event):
if event is InputEventMouseButton:
if is_pixel_opaque(get_local_mouse_position()):
print("sprite clicked")
Assessing this method
Pro: Simpler graph of nodes. Easy to set up. Con: Requires a custom script to be added and code to be added every time you want to use this method. It would be more convenient/reusable to simply add a “ClickDetector” node as a child.
00:37 But use the Action System instead!
It’s better not to hard code your inputs. Instead use the action system in Godot.
01:20 Area2D and CollisionShape method
Assessing this strategy
Con: 02:21 Doesn’t handle strange shapes very well.
02:27 Making Polygon Colliders in the Editor
- NOTE: You have to left-click to see the drop down menu.
Adjusting the CollisionPolygon2D
- Select your Sprite2D node in the scene tree.
- Click on the Sprite2D menu at the top of the 2D viewport.
- Choose “Create CollisionPolygon2D Sibling” from the dropdown menu In the preview window that appears, you can adjust three main settings:
- Simplification: Lower this value to create a more detailed collision shape that better matches your pixel art sprite. (Docs)
- Shrink (Pixels): Adjust this if you want the collision shape to be slightly smaller than the visible sprite.
- Grow (Pixels): Increase this if you want the collision shape to extend beyond the sprite’s edges.
For small pixel art sprites, try the following:
- Set Simplification to 0 or a very low value for maximum accuracy.
- Keep Shrink and Grow at 0 initially.
- Click “Update Preview” to see the changes in real-time
If the generated shape still doesn’t match your sprite perfectly, you can manually edit the CollisionPolygon2D after creation:
- Select the generated CollisionPolygon2D node.
- In the 2D viewport, you’ll see editable points on the polygon.
- Click and drag these points to adjust the shape manually.
- Right-click to add new points if needed.