I’m having problems with a raycast.
-
the script is attatched to the AI model,
-
the ray origin is the AI model and is directed towards the player.
The Ray however will not respond to anything that is within the Sphere Collider’s proximity.
(As seen in picture below) It detected the Cube outside the Sphere Collider but not the player within it.

var direction : Vector3 = other.transform.position - transform.position;
var angle : float = Vector3.Angle(direction, transform.forward);
if(angle < fieldOfViewAngle * 0.5)
{
var hit : RaycastHit;
if(Physics.Raycast(transform.position + transform.up, direction.normalized, hit, SphereCollider.radius))
{
Debug.Log(hit.collider.gameObject.tag);
}
}
I’m currently using parts of the code from (Unity) Project : Stealth, and in the tutorial the ray detects objects that are within its path (even if they’re inside the sphere collider).
Any help for fixing this is appreciated
You’ve given the Racast function a layermask argument, which tells it to ignore it. It’s the last argument. You should be able to just leave it out if you don’t want it ignored.
I’m a little unclear what is casting what from your picture there, but let me take a stab.
Could it be perhaps that your ray is coming OUT of the player?? Rays only collide with the outer facing portion of the collider: if you have a quad, it will only connect going “into” the visible portion, not coming out of it. Same goes for starting off inside geometry: you will not be able to sense coming out of it unless you also put inward-facing faces on your geometry, which you generally do not want to do.
I removed SphereCollider.radius from the code and the ray is still not detecting objects within the Sphere Collider.
Well I can simply set a Vector3.Distance to see if the player is nearby , but the collider method is quicker and easier to edit… if anyone knows why the raycast wont detect objects that are within the object’s Sphere Collider some help would be great
The only other thing there is the camera field of view statement, since the camera appears to be on the player, it may be affecting it. You could try making that if statement if(true) and see if it makes a difference, but save the code in case it doesn’t.