I have a flashlight attached to my first person controller and I can completely rotate the flashlight around. The flashlight is always rotating, (think of it as you are controlling a Wii remote and the light is pointing where the remote is pointing) and my ray cast is always facing only one direction. It is never facing forward where my flashlight is really pointing.
using UnityEngine;
using System.Collections;
public class FlashLight : MonoBehaviour {
public float rayDistance;
private float minIntensity = 0.5f;
private float maxIntensity = .75f;
private Ray daRay;
void Start () {
light.enabled = false;
}
void Update () {
RaycastHit hit;
daRay = new Ray(transform.position, Vector3.forward);
if(Input.GetKeyUp("f"))
light.enabled = !light.enabled;
if(light.enabled){
light.intensity = Random.Range(minIntensity,maxIntensity);
if(Physics.Raycast(daRay, out hit, rayDistance)){
if(hit.collider.tag == "Enemy")
Debug.Log("Shining the enemy :D");
}
}
Debug.DrawRay(transform.position,Vector3.forward * rayDistance);
}
}