- Home /
AI getting stuck on corners
Hi im new to game making and know 0 coding but "Understand" bits and pieces. I am attempting to make a chasing AI that always follows the player but when I turn a corner or in this case a hall with a few openings I turn the corner and it seems to follow me in a strait line instead of following as it should.
The code im using:
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Screen shots for example:
Answer by betaFlux · Apr 07, 2015 at 03:46 AM
The problem is, you only tell your ai to walk straight ahead towards the player, without taking obstacles into account. I guess what you want is pathfinding. That's where Unity's NavMeshAgent Component comes into play. It takes only four simple steps to make your ai go wherever you want.
Step 1: Make all obstacles in the scene 'static' (tick checkbox in the upper right corner in the inspector of the object) including walls and the ground of course.
Step 2: Click "Windows -> Navigation" and in the Navigation window that appears, click on "Bake" (bottom right corner), this will calculate all walkable areas. Don't forget to hit the bake button again, in case you change any of the navigation values.
Step 3: In addition to your custom ai script add a NavMeshAgent component to your ai character. (found under Component/Navigation/Nav Mesh Agent)
Step 4: Finally do some changes to your code:
var target : Transform;
var moveSpeed = 3;
var stoppingDistance = 0.1;
var myTransform : Transform; //current transform data of this enemy
private var _navMeshAgent : NavMeshAgent;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
_navMeshAgent = GetComponent(NavMeshAgent);
}
function Update () {
// make it so you still can handle the speed in the inspector of this component,
// just for simplicity's sake
_navMeshAgent.speed = moveSpeed;
// same goes for the distance where the ai should stop infront of the target
// and any other variables you might want to assign
_navMeshAgent.stoppingDistance = stoppingDistance;
// this line makes the ai constantly follow the target while avoiding obstacles
_navMeshAgent.destination = target.position;
}
I hope it helps!
Your answer
Follow this Question
Related Questions
What’s wrong with my Safe Volume (safe zone) code? 0 Answers
Enemy following Player in range 2 Answers
Enemy not rotating when inside range 1 Answer
Help with Enemy AI 1 Answer
Enemy AI With changing Player 0 Answers