Reaaally
Reaaally

Reputation: 3

Unity 3D 2D Enemy Patrol

This code is for enemy AI in unity 2D game. Right now the enemy is standing still but if i change both IF,s < or > then enemy is moving right or left direction and nothing more.

I am trying to make enemy AI patrol system. Got everything else done (enemy triggering system and enemy chasing player), but only this patrol part is not working.

I got one working patrol code in javascript but i need it in C# so everything would be inside one c# script file.

using UnityEngine;
using System.Collections;

public class VastaneAI : MonoBehaviour {

    protected Vector3 velocity;
    public Transform _transform;
    public float distance = 1f;
    public float speed = 1f;
    Vector3 _originalPosition;

    public void Start () {
        _originalPosition = gameObject.transform.position;
        _transform = GetComponent<Transform>();
        velocity = new Vector3(speed,0,0);
        _transform.Translate ( velocity.x*Time.deltaTime,0,0);
    }

    public void Update(){
        if (transform.position.x < _originalPosition.x-distance) {
            Debug.Log(_originalPosition.x-distance+"Left side");
            _transform.Translate ( (1*velocity.x)*Time.deltaTime,0,0);
        }   
        if (transform.position.x > _originalPosition.x+distance) {
            Debug.Log(_originalPosition.x+distance+"right side");
            _transform.Translate ( -velocity.x*Time.deltaTime,0,0);
            //velocity = velocity*-1;
        }   
    }
}

Upvotes: 0

Views: 8545

Answers (2)

Utsav Shrestha
Utsav Shrestha

Reputation: 77

Formic is right make a tiny change in his code and this will work.

public class enemy : MonoBehaviour {
protected Vector3 velocity;
public Transform _transform;
public float distance = 5f;
public float speed = 1f;
Vector3 _originalPosition;
bool isGoingLeft = false;
public float distFromStart;
public void Start () {
    _originalPosition = gameObject.transform.position;
    _transform = GetComponent<Transform>();
    velocity = new Vector3(speed,0,0);
    _transform.Translate ( velocity.x*Time.deltaTime,0,0);
}

void Update()
{    
    distFromStart = transform.position.x - _originalPosition.x;   

    if (isGoingLeft)
    { 
        // If gone too far, switch direction
        if (distFromStart < -distance)
            SwitchDirection();

        _transform.Translate (-velocity.x * Time.deltaTime, 0, 0);
    }
    else
    {
        // If gone too far, switch direction
        if (distFromStart > distance)
            SwitchDirection();

        _transform.Translate (velocity.x * Time.deltaTime, 0, 0);
    }
}

void SwitchDirection()
{
    isGoingLeft = !isGoingLeft;
    //TODO: Change facing direction, animation, etc
}
}

Upvotes: 0

Formic
Formic

Reputation: 650

The problem is that your code starts off with the position.x being neither less than or equal to the distance. If we use a state pattern we can clean this up a bit.

bool isGoingLeft = false;

void Update()
{    
    float distFromStart = transform.position.x - _originalPosition.x;   

    if (isGoingLeft)
    { 
        // If gone too far, switch direction
        if (distFromStart < -distance)
            SwitchDirection();

        _transform.Translate (velocity.x * Time.deltaTime, 0, 0);
    }
    else
    {
        // If gone too far, switch direction
        if (distFromStart > distance)
            SwitchDirection();

        _transform.Translate (-velocity.x * Time.deltaTime, 0, 0);
    }
}

void SwitchDirection()
{
     isGoingLeft = !isGoingLeft;
     //TODO: Change facing direction, animation, etc
}

Upvotes: 2

Related Questions