- Home /
Help with Enemy AI
So i made a basic "feeler" enemy AI that is only 2d which means it doesnt go up or down on the y axis. But now i would like to make it be able to walk up stairs or a ramp by felling around. Can anyone point me in the right direction on this? The 2d script im using now is:
var moveSpeed = 3;
var rotateSpeed = 10;
var checkDistance = 3;
var rightCheck : Transform;
var enemyChecker : Transform;
var leftCheck : Transform;
@HideInInspector
var target : Transform;
@HideInInspector
var controller : CharacterController;
function Start ()
{
target = GameObject.FindWithTag("Player").transform;
controller = GetComponent(CharacterController);
}
function Update ()
{
enemyChecker.transform.LookAt(target);
enemyChecker.transform.rotation.x = 0;
enemyChecker.transform.rotation.z = 0;
var hit : RaycastHit;
if (!Physics.Raycast(enemyChecker.transform.position, enemyChecker.transform.forward, hit, checkDistance))
{
transform.rotation = Quaternion.RotateTowards(transform.rotation, enemyChecker.transform.rotation, rotateSpeed/2);
}
if (Physics.Raycast(leftCheck.transform.position, leftCheck.transform.forward, hit, checkDistance))
{
transform.Rotate(Vector3.up * rotateSpeed);
}
if (Physics.Raycast(rightCheck.transform.position, rightCheck.transform.forward, hit, checkDistance))
{
transform.Rotate(Vector3.up * -rotateSpeed);
}
if (Physics.Raycast(leftCheck.transform.position, leftCheck.transform.forward, hit, checkDistance) && Physics.Raycast(rightCheck.transform.position, rightCheck.transform.forward, hit, checkDistance))
{
transform.Rotate(Vector3.up * rotateSpeed);
}
controller.SimpleMove(transform.forward * moveSpeed);
}
@script RequireComponent(CharacterController)
Can anyone point me in the right direction? Thanks!
Answer by fafase · Mar 27, 2013 at 07:00 PM
Two ways
You make the stairs small enough so that the CC can "climb up" -> not so good though is it?
You remove the colliders from the stairs and create a slope collider of one box collider inclined -> good point, it looks the same and your player walks fine on it. You can have a basic check to mak him play a walkingUpTheStairs animation despite the fact he is walking a flat slope. Bad point, you have to remove all colliders from your steps...
Thanks, but how could i tell the AI that he needs to go up the stairs? Because right now the enemy will go to underneath the player, The player is on the 2nd story and the AI will be right underneath him on the first story of the building.
Do you mean the AI is able to pass the stairs or walk the stairs depending on its state? If so, As you are in 2D, you could have the AI at z = 0 but the stairs at z=1 when the state is on chasing meaning you want him to go up, then you have a trigger box just at the bottom of the stairs that checks the state of the AI and "pushes" him of 1 in the z axis if he is chasing. Now your Controller will do the rest. At the top same process backward.
No it can walk up but the problem is that it doesn't know to walk up the stairs it just goes to underneath the player.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Accessing colliders, in if statement 2 Answers
help unity3d javascript 1 Answer
Make Raycast that ignores certain tags? 0 Answers
Raycasting2D please help 1 Answer