I must be doing something wrong here because my raycasts aren’t behaving correctly at all. I’m trying to make a system that detects if the player is in shadow by casting a ray from the light source to the player and seeing if it hits. My code is as follows:
using UnityEngine;
using System.Collections;
public class LightDetection : MonoBehaviour {
public Color cantSeeColour;
public Color canSeeColour;
public float range;
private Material material;
private GameObject player;
// Use this for initialization
void Start () {
material = GetComponent<Material> ();
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update () {
Vector3 direction = player.transform.position - transform.position;
RaycastHit hit;
Debug.DrawRay (transform.position, direction.normalized, Color.red, 1f);
if (Physics.Raycast (transform.position, direction.normalized, out hit, 25f)) {
if(hit.collider.gameObject == player)
{
Debug.Log ("I CAN SEE YOU");
} else {
Debug.Log ("Nothing to see here.");
}
}
}
}
However, instead of casting a ray for 25 units, it seems to only cast it for… 1. No matter what I set that 25f to, be it through hardcoding or variable, it’s always 1 unit long. So, what incredibly obvious thing have I done wrong here?
1f is the amount of time the ray persists, not the length of the drawray. If I change it to anything else, it just lasts longer, but doesn’t change in length.
huh, so it is… thats what assumption gets me, although it’s odd that raycasts have a specific parameter for length and the drawrays have it rolled into the direction parameter
edit: although, having said that, normalized direction would be of unit length, i.e. 1.
Okay, that solves the way the ray is drawn in the debug, but the length of the actual projected ray from Physics.Raycast is still only one unit long and doesn’t log “I CAN SEE YOU” unless I’m nearly on top of it.
using UnityEngine;
using System.Collections;
public class LightDetection : MonoBehaviour {
public Color cantSeeColour;
public Color canSeeColour;
public float range;
private Material material;
private GameObject player;
private CharacterController charController;
private float centreOfMass;
// Use this for initialization
void Start () {
material = GetComponent<Material> ();
player = GameObject.FindGameObjectWithTag ("Player");
charController = GameObject.FindGameObjectWithTag ("Player").GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
centreOfMass = charController.height / 2;
Vector3 direction = new Vector3 (player.transform.position.x, player.transform.position.y + centreOfMass, player.transform.position.z) - transform.position;
RaycastHit hit;
Debug.DrawRay (transform.position, direction.normalized * range, Color.red, 1f);
if(Physics.Raycast(transform.position, direction.normalized, out hit, range)) {
if(hit.collider.gameObject == player) {
Debug.Log ("I CAN SEE YOU");
} else {
Debug.Log ("Nothing to see here.");
}
}
}
}
I’ve updated the code slightly to better reflect in the drawray how things are supposed to work, but I’m still not getting the proper behaviour from physics.raycast. Any ideas?
Anyone? No matter what I seem to do, I can only register hits if I’m nearly right on top of the light casting the ray.
edit: I’ve changed the code again to show me where the ray is actually going and now I’m really baffled. It’s 100% colliding with the player, which is tagged “Player”, but it’s just not detecting them in the Hit until I’m right on top of the projector.
using UnityEngine;
using System.Collections;
public class LightDetection : MonoBehaviour {
public Color cantSeeColour;
public Color canSeeColour;
public float range;
private Material material;
private GameObject player;
private CharacterController charController;
private float centreOfMass;
// Use this for initialization
void Start () {
material = GetComponent<Material> ();
player = GameObject.FindGameObjectWithTag ("Player");
charController = GameObject.FindGameObjectWithTag ("Player").GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
centreOfMass = charController.height / 2;
Vector3 direction = new Vector3 (player.transform.position.x, player.transform.position.y + centreOfMass, player.transform.position.z) - transform.position;
RaycastHit hit;
Ray ray = new Ray (transform.position, direction.normalized);
if(Physics.Raycast(ray, out hit, range)) {
Debug.DrawLine(ray.origin, hit.point);
if(hit.collider.gameObject == player) {
Debug.Log ("I CAN SEE YOU!");
} else {
Debug.Log ("Nothing to see here.");
}
}
}
}