Hello,
Thanks for reading. I’ve created a laser sight in a 2d shooter using a line renderer, which is childed to the player. I’d like the laser sight to stop when it hits something. I tried using a raycast from the origin of the line renderer, in the direction the laser sight is pointed to figure out the distance of the hit object, but it isn’t hitting anything. Here’s the code I’m using. Can anyone help point me in the right direction?
RaycastHit hit;
// Convert central bulletspawn's quaternion to vector3
Vector3 laserSightDirection = bulletSpawn4.transform.rotation.eulerAngles;
Debug.Log ("PlayerScript.HandleLaserSight(): Laser sight direction is: " + laserSightDirection);
// Cast the ray from origin point along the direction of the laser sight
if(Physics.Raycast(laserSightObjectPistol, laserSightDirection, out hit, weaponRange * 25))
{
Debug.DrawLine(bulletSpawn4.transform.position, hit.point, Color.red);
Debug.Log("PlayerScript.HandleLaserSight(): Ray has hit " + hit.collider.gameObject.tag);
}