Hi all,
I have a script that basically says
When RIGHT CLICK IS HELD, RAYCAST in the direction the mouse is pointing to a distance of 10.0F UNITS. IF something is hit, RENDER A LINE between the ORIGIN and the HIT POINT. IF something is NOT HIT, RENDER A LINE between the ORIGIN and the END OF THE RAY.
For some reason, when my ray doesn’t hit anything, the line is rendered between the origin and a seemingly far away point. The strange thing is, I have a red Debug.DrawLine appear in order to show me the direction and magnitude of the raycast, and it seems like it’s always casting upwards? See gif below.
Here is my code:
void Update () {
//RIGHT CLICK CHECK
if (Input.GetMouseButton(1))
{
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Vector3 clickPoint = ray.origin;
clickPoint.z = 0;
CmdNawlinsLazer(clickPoint);
}
}
void CmdNawlinsLazer(Vector3 clickpoint)
{
RaycastHit2D hit = Physics2D.Raycast(gunTip.position, clickpoint - gunTip.position, 10.0F, whatLazerHits);
Debug.DrawLine(gunTip.position, clickpoint - gunTip.position * 10, Color.red, 0.5F);
print(hit.collider);
if(hit.collider == null)
{
print("NOT HITTING ANYTHING, RENDER LINE TO END OF RAY which is " + clickpoint - gunTip.position * 10);
//Enable the line renderer. Set two points. Render the line.
//_lineRenderer.enabled = true;
_lineRenderer.positionCount = 2;
Vector3[] points = new Vector3[2];
points[0] = gunTip.transform.position;
points[1] = clickpoint - gunTip.position * 10;
_lineRenderer.startWidth = 0.05F;
_lineRenderer.endWidth = 0.05F;
_lineRenderer.alignment = LineAlignment.View;
_lineRenderer.SetPositions(points);
//print(points[0] + " and " + points[1]);
}
else
{
print("HITTING SOMETHING AT" + hit.point);
_lineRenderer.positionCount = 2;
Vector3[] points = new Vector3[2];
points[0] = gunTip.transform.position;
points[1] = hit.point;
_lineRenderer.startWidth = 0.05F;
_lineRenderer.endWidth = 0.05F;
_lineRenderer.alignment = LineAlignment.View;
_lineRenderer.SetPositions(points);
}
}
I’ve seen this now in two of my scripts, so I think it may be a bug? I’ve been confused on this for about 4 days now. Hoping I can get some help here.