- Home /
Question by
Scruubbi · Mar 22, 2015 at 10:59 AM ·
raycastraycastallfog of war
Check when RaycastAll leaves an object
Ello peeps,
I'm making a MOBA game and I'm using a field of cubes that serves as my fog of war. I'm using a normal raycast to see if there is an obstacle that makes the viewing range smaller, after which I use a RaycastAll to find all the cubes that the raycast passes. However, the cubes only dissapear for a milisecond and then come back, while I need them to come back when the raycast doesn't pass through them anymore. Here's my script so far:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Fog Of War/Fog Of War")]
public class FogOfWar : MonoBehaviour {
public string fogCubeTag;
public LayerMask ignoredLayers;
public LayerMask ignoredLayers2;
void Update() {
RaycastHit hit;
Vector3 dir = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, dir, out hit, 10, ignoredLayers)) {
float dist = Vector3.Distance(transform.position, hit.point);
RaycastHit[] hits;
hits = Physics.RaycastAll(transform.position, dir, dist, ignoredLayers2);
int i = 0;
while(i < hits.Length) {
RaycastHit hit1 = hits[i];
GameObject cube = hit1.transform.gameObject;
if(cube) {
cube.GetComponent<MeshRenderer>().enabled = false;
}
i++;
}
}
}
}
Thanks in advance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613190745im_/https://answers.unity.com/themes/thub/images/avi.jpg)