Hi I am having a problem with Raycasting.
I have a player character represented by a cube and a patrol represented by another cube. The patrol cycles through waypoints while the player is free to walk around.
Attached to the Patrol cube I have another cube, set to act as a detection zone. When this zone is triggered, the patrol cube attempts to locate the player and fire a raycast at the player. This is being successfully achieved.
However the issue is, I want the player to be able to then move and hide itself from the view of the patrol. So when the player has an object positioned between itself and the patrol, the raycast can not see the player.
The code I am using according to the documents should not pass a ray through other objects with Colliders, however my code is doing just that.
(I have tried a few variations, so some of these are still there and commented out).
The Code
using UnityEngine;
using System.Collections;
public class PatrolDetection : MonoBehaviour {
public LayerMask sightBlockingLayers;
public RaycastHit hit;
public float maxRayCastRange = 500.0f;
//general detection
public bool isDetected = false;
//can see target
public bool canSee = false;
void OnTriggerStay(Collider other)
{
isDetected = true;
if (Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit))
//if (Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit, sightBlockingLayers))
//if(Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit, 100, sightBlockingLayers))
//if(Physics.Raycast(origin: transform.position, direction: (other.transform.position - transform.position), hitInfo: out hit, layerMask: sightBlockingLayers))
//if(Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit, maxRayCastRange, sightBlockingLayers))
{
//if(hit.collider.gameObject.name == "Player")
if(other.gameObject.name == "Player")
{
Debug.Log ("The " + other + " is staying in the collider");
canSee = true;
//Debug.Log ("I can see the " + other);
Debug.DrawLine (transform.parent.position, hit.point);
}
}
}
/*void OnTriggerEnter(Collider other)
{
if (Physics.Raycast(transform.position, (other.transform.position - transform.position), out hit))
{
if(other.gameObject.name == "Player")
{
Debug.Log ("The " + other + " entered the collider");
isDetected = true;
//Debug.Log ("I can see the " + other);
Debug.DrawLine (transform.parent.position, hit.point);
}
}
}*/
void OnTriggerExit(Collider other)
{
isDetected = false;
if(other.gameObject.CompareTag("Player"))
{
Debug.Log("The " + other + " exited the collider");
canSee = false;
}
}
}
This has me stuck, any advice would be most appreciated!
2h