2D Sidescrolling AI Script

I am having some problem to get AI for my 2d side scrolling game to work, Ive tried using the AI from the fps Tutorial and using waypoints but does not work very well and doesn’t stay on the same Z axis, even when I put the local position of Z to 0 its not very good. I seen a prefab on AI for 2D games but don’t have paypal and would rather figure this out to improve my scripting knowledge. How would I make the AI jump when it must?

If anyone can help me out, thanks!

The // … bits below are omitted sections of code, not descriptions. You’d have to insert basically your AI’s thinking into those areas.

But getting it to jump and stay on one plane isn’t terribly difficult:

private var localYVelocity : float = 0.0;
var jumpPower : float = 1.0;
private var canJump : boolean = false;
private var onGround : boolean = false;

function Jump() {
 localYVelocity = jumpPower;
 canJump = false;
 while ( !onGround ) yield;
 canJump = true;
}

private var desiredMoveVector : Vector3;
var worldZPosition : float = // whatever plane it is your stuff is on

function Update() {
 // ... determine desiredMoveVector
 localYVelocity += 9.81 * Time.deltaTime;
 desiredMoveVector.y = localYVelocity;
 desiredMoveVector.z = 0;
 // ... perform move function
 transform.position.z = worldZPosition;
 // ... determine whether I want to jump or not
 var wantToJump : boolean = true;
 // ... set onGround to true if on ground
 if ( wantToJump  canJump ) Jump();
}

You can use triggers to send a jump message to your AI in places they would need to jump. For keeping the object on the 0 Z axis do something like this:

void LateUpdate()
    {
        // Make sure we are absolutely always in the 2D plane.
        transform.position = new Vector3(transform.position.x, transform.position.y, 0);
    }

the code works but makes the enemy slow down and get stuck in the air, kind of like the script is squeezing the enemy to stay in the 2d plane which is causing it to unable to move or move very very slowly… I think its fine if i keep the waypoints on Z = 0 that way the enemy will walk accordingly.

I think my main concern is making ai jump when it needs, I am thinking an algorthm like a short raycast distance infront of the ai and if hes close enough and sees something, he may jump. I just cannot script this.

Right; the script zeroes all Z-axis movement so things stay exactly on the z axis.

As for “wantToJump” -

if (!Physics.Raycast( transform.position + transform.forward, -Vector3.up, /*character height*/ ) ) {
  // there's a pit in front of me!
  wantToJump = true;
}

That’ll always jump at the edges of platforms, if you replace character height with how far it is from the character to a bit below its feet (so it doesn’t jump on slopes)

Great I got it to work, but just a small problem, the AI seems to jump even on the slightest slopes because its detecting that the slope is “in the way” thats why he jumps…

So i decided to ignore layer mask to ignore terrain for example, but im not sure how to script it, I have this…

if (Physics.Raycast (transform.position, fwd, 2.5, layerMask)) {

But this way, it will only recognize things in layer 8, not IGNORE layer 8… so ive got it in reverse, how do i change it?

You can use the ~ operator (bitwise NOT operation) to reverse all the bits in an integer:-

layerMask = ~layerMask;

This works perfectly but cant be in update function, it can just be next to the defined layermask variable

Its exactly what i need, thanks andeeee!!