# Unity C# 2D Platformer Enemy Patrolling

I'm setting up a basic mario-like 2D platformer and I'm trying to give 'enemies' a patrolling path on a platform. (just like the turtles in Super Mario Bros). All they do is patrol from left to right and back. <--->

Currently my game object is walking to left and right in the middle of my scene. Despite me positioning my game object as far left as I can, at first it walks to the center of the scene and continues acting out his patrol in the center of my scene.

What I can't seem to figure out is adjusting the position where he patrols. I don't want him to patrol in the center of the scene.

I think I should use the originalX position in the Update function but I'm not sure how (I could be wrong)?

This is the script that is linked to my enemy gameobject.

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
public float walkSpeed = 1.0f;      // Walkspeed
public float wallLeft = 0.0f;       // Define wallLeft
public float wallRight = 5.0f;      // Define wallRight
float walkingDirection = 1.0f;
Vector2 walkAmount;
float originalX; // Original float value

void Start () {
this.originalX = this.transform.position.x;
}

// Update is called once per frame
void Update () {
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= wallRight) {
walkingDirection = -1.0f;
} else if (walkingDirection < 0.0f && transform.position.x <= wallLeft) {
walkingDirection = 1.0f;
}
transform.Translate(walkAmount);
}
}

• This is really a debugging problem. Try putting debug.logs in all your logic places to make sure the game is doing what you think it's doing. Try drawing boxes in the scene view at the positions you think things are happening. Try looking in the inspector to make sure your member variables are numbers you think they should be. – Tetrad Apr 3 '15 at 19:17

It looks like wallLeft and wallRight are absolute positions--they aren't relative to your AI's initial position. So the AI starts wherever, and then walks towards it's absolutely positioned bounds, and then patrols in there.

I would try something like this:

void Start () {
wallLeft = transform.position.x - 2.5f;
wallRight = transform.position.x + 2.5f;
}

• is there a way to change the value of 2.5 within the Unity editor? – Caspert Apr 3 '15 at 19:55
• It's been a while since I used Unity, but I think you could add a public float patrolWidth and then use wallLeft = transform.position.x - patrolWidth / 2 and wallRight = transform.position.x + patrolWidth / 2. Basically you need to turn the 2.5f into a public field so that Unity will pick it up and include it in the editor. – ricksmt Apr 3 '15 at 19:59
• I had set the wallRight to a public float, but then you will see negative values in the editor, and can't put 1 for example. That's than to far for movement.. That value is calculated from the center of the scene.. Don't know if it is possible, to set the value of wallRight, from the original start position of the object? @ricksmt – Caspert Apr 3 '15 at 20:07
• Despite what you initially set for wallRight, the Start method will override it based on the calculation, and if your AI start at a negative x you could easily get a negative number for wallRight. I wouldn't expose wallRight or wallLeft, but instead expose patrolWidth. – ricksmt Apr 3 '15 at 20:12

Try this:

void Update () {
walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
if (walkingDirection > 0.0f && transform.position.x >= originalX + wallRight) {
walkingDirection = -1.0f;
} else if (walkingDirection < 0.0f && transform.position.x <= originalX - wallLeft) {
walkingDirection = 1.0f;
}
transform.Translate(walkAmount);
}


It seems forgot to use the original position of the character, when checking for patrolling limits.