Hey y'all!
I'm stuck on a Unity 2D problem I can't figure out.
Basically, I'm working on a
Worms-like ninja rope for my current project, and I'm stuck.
The algorithm goes as follows (pseudo-code):
public List<Vector3> pointList;
void FixedUpdate() {
/* Create point */
Vector3 lastPoint = this.getLastPoint();
RaycastHit2D rch = Physics2D.Raycast(this.player.transform.position, lastPoint - this.player.transform.position, Vector3.Distance(lastPoint, this.player.transform.position) - 0.01f, layerMask);
if (null != rch.collider) // Point found
this.addPoint();
/* !Create point */
/* Delete point */
Vector3 preLastPoint = this.getPreLastPoint();
RaycastHit2D preRch = Physics2D.Raycast(this.player.transform.position, preLastPoint - this.player.transform.position, Vector3.Distance(preLastPoint, this.player.transform.position) - 0.01f, layerMask);
if (null == preRch.collider) // Point found
this.removeLastPoint();
/* !Delete point */
}
Basically, if there's an obstacle between the player position and the last stored point, add a point there. If there is no obstacle between the player and the second-last point, remove the last point (because it's not needed anymore).
The problem arises when a platform is too small but there's no collision with the second-last point. Here is an example:
As you can see, before, the rope is below, but since there's no obstacle between the player and the second-last point, the last point gets deleted.
Now, the last image shows what I'm trying to detect: whether there is an obstacle in the show triangle.
And man, I've tried everything I know.
Physics2D.CircleCastAll and
Physics2D.BoxCastAll give me nothing because, most of the time, they find inaccurate results (more like all the time, really) and, even if they do, I wouldn't necessarily get points in the platforms closest to be inside the triangle.
I don't want to iterate through all the platforms to check the triangle/platform collision because it will really mess with my game performance (especially since it's a mobile game). But I'm stuck here, I don't know why the circle and box raycasting don't work and I can't find an elegant solution to my issue.
Does anyone have any kind of idea how to detect this?
Thanks!