- Home /
Enemy Sighting Using SphereCast
Hi, I'm creating an FPS and I was working on an AI which can the player/other AI when they're in front of him and then would attack, but physics.sphereCast isn't working. Please show me where I'm going wrong. here's my code (in JS)
var fieldOfView : float;
function Update(){
var pos : Vector3 = transform.position;
var direction : Vector3 = targetPos.transform.position - transform.position;
var angle : float = Vector3.Angle(direction, transform.forward);
if(angle < fieldOfView * 0.5f){
var hit : RaycastHit;
if (Physics.SphereCast(pos, 100, transform.forward, hit)) {
if(hit.collider.tag == "BlueTeam"){
Debug.Log("CanSee");
}
}
}
}
What's wrong? Try always printing hit.transform.name
. $$anonymous$$aybe it always hits the ground and you need to raise where you shoot from. $$anonymous$$aybe it always misses. $$anonymous$$aybe your FoV always stops it (another debug.log to check.) $$anonymous$$aybe you are always hitting a giant trigger box you forgot about.
Raycasts are probably better for "seeing." If you sphereCast to an enemy head, just over a wall, it will hit the wall. A sphereCast at a known object is like saying "make sure I can see it, with this much extra room." Checking if you can see any part of them involves extra raycasts, to each part.
Your answer
Follow this Question
Related Questions
Sphercast hitinfo TextureCoord rounded? 1 Answer
anyone know the maths to take into account Bounciness when calculating trajectories? 1 Answer
Why is my spherecast returning an incorrect hit on mesh collider? 1 Answer
Physics.SphereCastAll - how far did the trace travel? 3 Answers
How can the resultant velocity of an object after a potential collision be predicted? 1 Answer