I have a bit of a problem here, first off. My desktop’s graphics card for whatever reason can’t render shadows.
Anyways, I’ve started on a script to detect if the player is in the shadows or not. Here’s what I have so far:
using UnityEngine;
using System.Collections;
//[RequireComponent(typeof(DayHud))]
public class DetectPlayerVisibility : MonoBehaviour {
[SerializeField]
Transform WorldLight;
Vector3 RayDir;
DayHud dayHud;
// Use this for initialization
void Start () {
// Ray direction is SUPPOSED TO BE at the same angle as the Directional Light, but pointing the opposite direction
RayDir = new Vector3 (WorldLight.rotation.x * -1, WorldLight.rotation.y, WorldLight.rotation.z * -1);
//Get the Day HUD from the Camera
dayHud = Camera.main.gameObject.GetComponent<DayHud> ();
}
// Update is called once per frame
void Update () {
if (Physics.Raycast (transform.position, RayDir, 1000))
{
dayHud.IsHidden = false;
print ("Player is not visible.");
} else {
dayHud.IsHidden = true;
print ("Player is visible.");
}
}
void OnDrawGizmos()
{
Gizmos.DrawRay (transform.position, RayDir);
}
}
My problem is no matter what I do to WorldLight.rotation when setting RayDir, the ray is always facing a completely different direction.
I just need it to basically be at the same angle as the directional light, except aimed at the sky instead of the ground.
Well. I managed to figure this out and the answer was INCREDIBLY simple. I don’t know how I missed it.
I just had to use WorldLight.forward instead of RayDir. And multiply it by -1.