Issue drawing a prediction path.
Edit: fixed by changing:
RaycastHit2D hit = Physics2D.Raycast(start_position , direction, 20, collision_layer);
to:
RaycastHit2D hit = Physics2D.Raycast(start_position + (Vector3)direction.normalized, direction, 20, collision_layer);
Hi, I'm getting some odd behavior while trying to draw a prediction path.
There is a video showing the issue here.
What should happen:
Click to set anchor point.
Drag to create direction vector.
Raycast along the direction vector until something is hit, use that hit point as a new anchor point and a reflection of the current direction across the normal of the hit as the new direction, add the hit point to the path list.
Continue doing this until nothing is hit or the maximum number of reflections is reached.
Set LineRenderer positions to the recorded hit points.
The problem is that sometimes the reflection just doesn't seem to happen, it hits the collider and stops, I'll then aim a bit further up or down and it will start reflecting again.
The two barriers that can be seen in the video just have 2D box colliders on them so there shouldn't be any strange angles being produced to throw it off.
Here is the code I'm using:
void Draw_Paths()
{
List<Vector3> paths = new List<Vector3>();
Vector3 start_position = this.transform.position;
//start_touch_position is the space on screen the first mouse press took place
Vector2 direction = (Vector2)start_touch_position - (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
paths.Add(start_position);
for (int i = 0; i < 10; i++)
{
//collision layer is objects off of which the raycast should bounce, in the video this is just the white barriers on either side of the screen
RaycastHit2D hit = Physics2D.Raycast(start_position, direction, 20, collision_layer);
if (hit != false)
{
paths.Add(hit.point);
start_position = hit.point;
direction = Vector2.Reflect(direction, hit.normal);
}
}
aim_line.positionCount = paths.Count;
for (int j = 0; j < paths.Count; j++)
{
aim_line.SetPosition(j, paths[j]);
}
}
Posted Jun 17, 2017, 11:38 AM UTC · edited