How can I make the I see the player better?

I’ve been making a first person shooter game using Unity. I recently released an alpha of the game to get feedback. One of the most reported problems is that the enemy can’t see you unless you’re right in front of them. I noticed this myself.

The problem is that they are using a raycast to see the player, and have a very narrow field of view, so I want to know, is there a better way to get the AI to see you without having the game take a big performance hit? I’m going to have a lot of enemies in my games, so performance is important for me.

I was thinking of using trigger colliders to spot the player, would that work, or is that too costly?

1 Answer

1

Well, you could use Vector3.sqrMagnitude between the position of the NPC and the Player and set a certain threshold, when the NPC will activate. If you really want to optimize that, you have to separate your map into a grid or use binning.

Well, okay, how would I do either? Are there any tutorials?

–

Well for the Vector3.sqrMagnitude, you should use this on your enemy control script and you should have a reference to the player: Vector3 playerPosition = player.transform.position; Vector3 npcPosition = transform.position; Vector3 distanceVector = playerPosition - npcPosition; float sqrDistance = distanceVector.sqrMagnitude; if(sqrDistance < SOME_DEFINED_RADIUS) { // player detected }

–

The distance solution is efficient, but it will let them see through obstacles. So once that triggers you also need to add a Physics.Raycast toward the player's position and see if it hits a wall before it hits the player.

–