Question by
StreakFlash · Jul 22, 2018 at 06:59 AM ·
raycastraycasthitcrosshairtrail
Follow crosshair trail
Hi,
I am trying to make a laser guided missile which will follow the direction of my screen center (crosshair). To accomplish that I tried to use RayCasting and it does work BUT only for the colliding objects, once the corsshire is pointed into the air (void) Physics.Raycast
returns RaycastHit(0.0,0.0,0.0)
and I am losing the track of the crosshair. Is there anything I can do to make the raycast work in void or should i be using other functionality? please advice, thanks!
public class RayCasting : MonoBehaviour {
public Camera FPSCamera;
public GameObject targetPointerPrefab;
private GameObject targetPointer;
void Start () {
targetPointer = Instantiate(targetPointerPrefab, transform.position, transform.rotation);
}
void Update () {
UpdateTargetPointer();
}
void UpdateTargetPointer()
{
RaycastHit hit;
if (Physics.Raycast(FPSCamera.transform.position, FPSCamera.transform.forward, out hit))
{
targetPointer.transform.position = hit.point;
}
else
{
// Guess something can be done here... take the camera position and multiply by total distance ?
}
Debug.Log(hit.point);
Debug.DrawRay(transform.position, FPSCamera.transform.forward * 1000, Color.red);
}
}
Comment