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!
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.
– TheacesofspadesDo 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.
– fafaseNo 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.
– Theacesofspades