- Home /
SphereCast misses but RayCast and OverlapSphere don't
I'm brand new to game dev so bear with me.
code in question:
Debug.DrawRay(controller.Eyes.position, controller.Eyes.forward.normalized * controller.Stats.LookRange, Color.green);
var overlappingColliders = Physics.OverlapSphere(controller.Eyes.position, controller.Stats.LookSphereCastRadius);
var rayCastResult = Physics.Raycast(controller.Eyes.position, controller.Eyes.forward.normalized, controller.Stats.LookRange);
var sphereCastResult = Physics.SphereCast(
controller.Eyes.position,
controller.Stats.LookSphereCastRadius,
controller.Eyes.forward.normalized,
out hit,
controller.Stats.LookRange);
if (sphereCastResult
&& hit.collider.CompareTag(
(controller.TeamTag == Tags.BlueTeam)
? Tags.RedTeam
: Tags.BlueTeam))
{
// we see a target that is not on my team
controller.CurrentTarget = hit.transform;
return true;
}
This is pulled from the "PluggableAI" tutorial and works in that project, but not in my project.
I expect my object to see a Cube Collider. I can see it hitting it with Debug.DrawRay
. RayCast
returns true
and OverlapSphere
shows the collider as well. So why does SphereCast
pass straight through that collider and continue on all the way to the "South Wall" collider a long way across the map?
Answer by Cornelis-de-Jager · Apr 20, 2018 at 04:59 AM
Hi mate, I think the issue is with your
&& hit.collider.CompareTag(
(controller.TeamTag == Tags.BlueTeam)
? Tags.RedTeam
: Tags.BlueTeam)
try removing that and see if the Spherecast hits then. Because It looks like it should.
I thought so too which is why I moved the sphereCastResult
to it's own line outside the original If
statement.
If you look at line 12 if (sphereCastResult
that value for sphereCastResult
is already false
so the tag check doesn't interfere at all.
Well....turns out you were right. It also turns out the code I pasted above wasn't exactly what I was working with at the time. I apologize for that.
Your answer
Follow this Question
Related Questions
How to scan a surface of a GameObject and get the distance of every edge to camera 1 Answer
Should Items spherecast 0 Answers
How to Spherecast/Raycast diagonally? 1 Answer
Problem Standing into Objects 0 Answers
Generating lines between procedurally instantiated gameobjects, within a certain radius 1 Answer