Enemy teleporting problem. HELP (C#)

Hello everyone and thanks for reading this :slight_smile: So I made a script for horor AI and AI is teleporting randomly in the map. So the problem is when enemy teleports it can be teleport to the inside of objects. I mean I can see enemies body is half on the outside half is inside of the car. How I can fix this. Thanks and sorry for my english :slight_smile:

transform.position = new Vector3(Random.Range(player.position.x + mesafe, player.position.x - mesafe), Random.Range(player.position.y, player.position.y), Random.Range(player.position.z + mesafe, player.position.z - mesafe));

How about something like this:

var newPos = new Vector3(Random.Range(player.position.x + mesafe, player.position.x - mesafe), Random.Range(player.position.y, player.position.y), Random.Range(player.position.z + mesafe, player.position.z - mesafe));

// This will check to see if there is anything in the way before teleporting there.
if (!Physics.CheckCapsule(newPos - Vector3.up, newPos + Vector3.up, 0.5)) {
      transform.position = newPos;
}else{
      // Do something else. Maybe restart the teleport function to try again.
}

I suggest you read about Physics.CheckCapsule in the docs.

P.S. Your english is fine

Hello! Is newPos going to be bool or vector 3? Because In the docs it says bool or I dont get it :slight_smile: @GameScience

newPos is Vector3.

Physics.CheckCapsule is bool.