- Home /
RayCast Boolean Help
Hey guys, I have a RayCast problem. First let me explain what I want. You kill the zombie, and then in a few seconds the zombie will respawn in the same location. Now for obvs. reasons I dont want the zombie to spawn if the player is looking directly at him.
So I set up a RayCast where I say "If the players raycast is colliding with a zombie then set spawnSight = true and if it is not set spawnSight = false. If spawnSight = true my Respawn will not Respawn
I dont know if I havent had enough sleep or what but my code doesnt seem to work when the RayCast collides spawnSight is set to true, however when the RayCast leaves the collider it stays at true
Sorry if this is a dumb question any help would be appreciated thanks again!
using UnityEngine; using System.Collections;
public class RayCast : MonoBehaviour {
float distance = 40;
Vector3 direction;
RaycastHit hit;
GameObject zombie;
public static bool spawnSight;
void Start()
{
zombie = GameObject.FindWithTag("Enemy");
}
// Update is called once per frame
void Update () {
Debug.Log(spawnSight);
direction = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, direction * distance, Color.blue);
if(Physics.Raycast(transform.position, direction, out hit, distance))
{
if(hit.collider.gameObject == zombie)
{
spawnSight = true;
}
}
else
{
spawnSight = false;
}
}
}
Answer by Xathereal · Nov 06, 2012 at 10:09 PM
Try using "Debug.Log" to print out the "hit" data, like the gameObject name.
It might be hitting something else, so it wouldn't change the 'spawnSight' to true because it isn't a zombie and wouldn't change it to false because it hit something.
Try something like this and you will probably find your answer:
if (Physics.Raycast(transform.position, direction, out hit, distance))
{
Debug.Log(hit.collider.gameObject);
if (hit.collider.gameObject == zombie)
{
spawnSight = true;
}
else
{
spawnSight = false;
}
}