- Home /
Top down shooter: Laser Sight
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);
}
Answer by robertbu · Jul 10, 2013 at 11:15 PM
This is not how you calculate direction. Replace line 4 with:
Vector3 laserSightDirection = bulletSpawn4.transform.forward;
Your code is a bit strange in that the position argument is based off of 'laserSightObjectPistol' but your direction is based off of bulletSpawn4. Usually you use the same object for both position and direction.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105851im_/https://answers.unity.com/themes/thub/images/avi.jpg)