- Home /
Line renderer is not following raycast?
I'm using the below code to create a raycast laser, however the laser endpoint doesn't end up at the hit point. Instead it goes behind my player in the opposite direction. I've confirmed the raycast is working properly through Debug.Drawray. What could be wrong?
IEnumerator PlatformerRaycastShot(){
Vector3 vectorToTarget = crosshair.transform.position - transform.position;
vectorToTarget.Normalize();
RaycastHit hit;
if(Physics.Raycast(transform.position, vectorToTarget, out hit, 5000, ~IgnoreMe)){
Debug.Log("2d player hit " + hit.transform.name);
Debug.DrawRay(transform.position, vectorToTarget * 1000, Color.green);
laserLine2D.gameObject.SetActive(true);
laserLine2D.SetPosition(1, hit.point);
}
yield return new WaitForSeconds(5);
lastShootTime = Time.time;
}
Answer by daotanp · Jan 04 at 06:31 AM
You can use Debug.DrawLine(transform.position, hit.point, Color.green);
to see if the hit point is exactly where it supposed to be.
I figured it out, had to check Use World Space on the Line Renderer.
Thanks!
Answer by MILLIMEDIA · Jan 04 at 12:01 AM
What is the purpose of the * 1000
in this line Debug.DrawRay(transform.position, vectorToTarget * 1000, Color.green);
? Unless I'm missing something, that might be your problem
vectorToTarget is a direction, so its magnitude is 1, 1000 is basically the length of the ray
Debug does not do anything to the code and often be used to see if things are working correctly