2D top down Enemy AI Needed

so i found this enemy ai code. but when i use it, it seems to confliuct with my player movement code, and they move at the same time but slightly out of sync

Enemy AI code:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
    public Transform target;
    public int moveSpeed;
    public int rotationSpeed;
    public int maxdistance;
   
    private Transform myTransform;
   
    void Awake(){
        myTransform = transform;
    }


    void Start () {
        GameObject go = GameObject.FindGameObjectWithTag("Player");
       
        target = go.transform;
   
        maxdistance = 2;
    }
   

    void Update () {
        Debug.DrawLine(target.position, myTransform.position, Color.red);
       

        myTransform****tation = Quaternion.Slerp(myTransform****tation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
       
        if(Vector3.Distance(target.position, myTransform.position) > maxdistance){
        //Move towards target
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
   
        }
    }
       
}

and this is my player code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

   
    public float moveSpeed;

    private Animator anim;
    private Rigidbody2D myRigidbody;

    private bool playerMoving;
    private Vector2 lastMove;

    // Use this for initialization
    void Start () {

        anim = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();

    }

    // Update is called once per frame
    void Update() {

        playerMoving = false;

        if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {

            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
            myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal"), myRigidbody.velocity.y);
            playerMoving = true;
            lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
        }


        if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
        {
             transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
            myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * moveSpeed);
            playerMoving = true;
            lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
        }

        if(Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
        {
            myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
        }

        if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
        {
            myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
        }



            anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
        anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
        anim.SetBool("PlayerMoving", playerMoving);
        anim.SetFloat("LastMoveX", lastMove.x);
        anim.SetFloat("LastMoveY", lastMove.y);
    }
}

Hello Mongpong!

Could you be more specific about what is wrong and what is the result you expect from the AI?

basic random motion, and for the ai to attack the player within a radius

my ai doesnt seem to be doing much at all apart from being controlled the same as the player

Ok, so from what I can see in the AI script, what makes it move is this part:

if(Vector3.Distance(target.position, myTransform.position) > maxdistance)
{
        //Move towards target
        myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

Now, if you want your AI to move towards the player (and attack him) only when the player is within the radius of the AI, you could attach to the AI a Sphere Collider, and make the AI move towards the player only when he collides with that Sphere Collider (using the method OnTriggerStay and OnTriggerExit for example).

bool isInRadius = false;

void Update ()
{
    if(Vector3.Distance(target.position, myTransform.position) > maxdistance && isInRadius == true)
        {
            //Move towards target
            myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        }
}

void OnTriggerStay (Collider ai) //isInRadius will stay true as long as the Player is within the radius
{
    If (ai.CompareTag("Player"))
        isInRadius = !isInRadius;
}

void OnTriggerExit (Collider ai) //isInRadius will become false when the Player exit the radius of the AI
{
    if (ai.CompareTag("Player"))
        isInRadius = !isInRadius;
}

Also, don’t forget to tag your Player game object as “Player”.

As for random motion, there’s a “Wander” script on the Unity wiki, that you can find right here.

1 Like