top down ai

Hi im trying to create a top down ai and am unsure how i would go about doint it it needs to be 4 directional like my player movement

void Update() {
		if (photonView.isMine) {
			if (Input.GetKey (KeyCode.W)) {
				GetComponent<PhotonView> ().RPC ("StateForward", PhotonTargets.All);
				rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;
				GetComponent<PhotonView> ().RPC ("Animations", PhotonTargets.All);
			} else if (Input.GetKey (KeyCode.S)) {
				GetComponent<PhotonView> ().RPC ("StateBackward", PhotonTargets.All);
				rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
				GetComponent<PhotonView> ().RPC ("Animations", PhotonTargets.All);
			} else if (Input.GetKey (KeyCode.A)) {
				GetComponent<PhotonView> ().RPC ("StateLeft", PhotonTargets.All);
				rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime;
				GetComponent<PhotonView> ().RPC ("Animations", PhotonTargets.All);
			} else if (Input.GetKey (KeyCode.D)) {
				GetComponent<PhotonView> ().RPC ("StateRight", PhotonTargets.All);
				rigidbody2D.transform.position += Vector3.right * speed * Time.deltaTime;
				GetComponent<PhotonView> ().RPC ("Animations", PhotonTargets.All);
			} else {
				GetComponent<PhotonView> ().RPC ("AnimationsOff", PhotonTargets.All);
			}
			}
		}

i am doing the ai with a circle collider that detect the player

using UnityEngine;
using System.Collections;

public class AiMovement : MonoBehaviour {

	public float speed;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}


	void OnTriggerEnter2D(Collider2D other) 
	{
		if (other.CompareTag("Player"))
		{

		}
	}
}

How do i determin what direction the player is in so that i can move down or up and lef and right acordingly?

Hi. I’m not sure if this is still being watched but I have a solution to your problem. I’m currently working on a game for my sister that uses the same 4 directional AI steering solution that you are trying to achieve. However it is much more than simply checking if X is greater than X and Z is greater than Z. Nope.

The above posts will work fine for obstruction free worlds, unless you want to incorporate lots of RayCast calls. But that can get pretty heavy. I wrote a script yesterday that allows AI to find the closest path to their target on your navigation mesh, but restricting their movement from omnidirectional to quad-directional.

This is done by using a NavMeshAgent for the AI with the speed, acceleration and angularSpeed set to 0f. This way the AI will not move using the agent. Instead, my script takes the automatically generated path from the NavMeshAgent into the script and compare the X and Z distance values of the closest corner target. If X is closer than Z, then the Ai will move left or right depending on if the value returns negative or not, then when it gets as close to the target on the X axis as it can, it begins to move on the Z axis.

The AI does this, one destination at a time until it reaches its target. It’s actually pretty simple when you start to really look at it.

I’m going to attach the script I’m using in my project for you so that you can dissect it and learn what I did. If you want to use this script in your game, just make sure you bake a navigation mesh into your scene. otherwise the AI will have no pathfinding.

Since we are both working on the same type of game, maybe we can help eachother out. Just let me know if you have any questions. It’s been a couple of months since this question was posted so I hope someone sees this. LOL.[56720-quaddirectionalnavigation.txt|56720]