0
\$\begingroup\$

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);
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Tetrad Apr 3 '15 at 19:17
0
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ is there a way to change the value of 2.5 within the Unity editor? \$\endgroup\$ – Caspert Apr 3 '15 at 19:55
  • \$\begingroup\$ 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. \$\endgroup\$ – ricksmt Apr 3 '15 at 19:59
  • \$\begingroup\$ 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 \$\endgroup\$ – Caspert Apr 3 '15 at 20:07
  • \$\begingroup\$ 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. \$\endgroup\$ – ricksmt Apr 3 '15 at 20:12
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.