- Home /
Question by
ExpiredIndexCard · Apr 01, 2013 at 11:43 PM ·
raycastlightrayhow to
My raycast is not always in the forward direction.
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);
}
}
Comment
Best Answer
Answer by Chronos-L · Apr 02, 2013 at 12:40 AM
Change:
daRay = new Ray(transform.position, Vector3.forward);
To
daRay = new Ray(transform.position, transform.forward);
When you put Vector3.forward
, the ray will always face the +Z axis.
Your answer
Follow this Question
Related Questions
Raycasting error? 1 Answer
camera.ScreenPointToRay always has same origin... 1 Answer
How can I can I cast a ray from a gameobject? 1 Answer
ray direction to quaternion 0 Answers
GameObject follow mouse 1 Answer