- Home /
Raycasting is locking to a certain position (is this a bug?)
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.
Answer by Glurth · Sep 04, 2017 at 10:15 PM
clickpoint - gunTip.position
While this expression IS correct for defining a Ray's direction:
Physics2D.Raycast(gunTip.position, clickpoint - gunTip.position, 10.0F, whatLazerHits);
It is not the correct expression for the line renderer: that expression should be the actual location you want to draw to (`clickpoint`). If you want the line 10 times the distance of click point away:
offset = (clickpoint - gunTip.position) * 10; //PARENS here are *critical*
endpoint = clickpoint + offset;
points[1] = endpoint ;
Dude, you're awesome. Thank you so much. Works beautifully. Now I'm going to read this over to try to understand this.
Cheers!
" try to understand " things to keep in $$anonymous$$d, study: A Vector CAN represent a position in 2d or 3d space. It can also represent an OFFSET (translation) to a position. In this form we are using the vectors direction and the magnitude(length). Offset vectors are generally only useful when used in conjunction with a position vector.
An offset vector and a position vector, are basically the same EXACT thing: except that the "position" vector is ALWAYS used as an "offset" from the ORIGIN (0,0,0). This is why we can subtract one "position" from another "position", to get an "offset".
"Order of operations": you need to know these like the back of your hand. (why parens are needed).