I have an Area2D representing the hitbox of the player’s attack. If the attack overlaps with a node in the group, ‘solid’, then I want to create an effect at the closest point on that collider. I am currently using a triangular shape-cast, which I think is overly convoluted and it is giving me some weird behaviour. Unity has a simple Collider.ClosestPoint, which would be a better solution. How would yall go about achieving this?
I agree with @OriginalBadBoy here; raycasting or shapecasting is the straightforward solution to your problem.
While I can’t imagine why you would use a square shape to approximate the capsule area, using a ShapeCast2D along with its get_collision_point() should provide you with a solution capable of determining the closest impact point relative to the shapecast’s origin.
Would it be possible for you to explain why:
You’re using a square shape?
What is not working with your current shapecast approach?
It was creating some strange behaviour like if I cast through a wall it would ignore the wall and hit the next target. Also, if I want to create an effect for each collider, like if you hit two enemies at once, then a shapecast wouldn’t work as I would only be able to get the first point. This is also a more general issue, I want to know if there is a way to find the closest point on a collider.
Try this: A ShapeCast2D, then use a Circle shape, increase its radius, and pull back the target position to 0,0. Then you could do something like this to get the nearest collision point to any surface intersecting that circle:
var origin = $ShapeCast2D.global_position
var points = []
for i in $ShapeCast2D.get_collision_count():
points.append($ShapeCast2D.get_collision_point(i))
print(points.min())