I dont get AI

I have been trying to find out how Ai works
Im trying to make a Ai that finds the players position then moves towards it with out rotating

Oh and a lot of what i have seen is
public transform target;
and
void start() {
target = GameObject.Find(“Your_Name_Here”).transform.position;
}
I just dont get them and how they work and another thing i dont understand is. is there a way to get another gameobject’s
position and be able to separate out the x y and z axis. that would be helpful

Basically what you describe is one of the fundamentals of games involving moving objects.

First learn how to move an object in a linear direction using code, by adding a small offset to its position each frame.

Then learn how to choose the amount of that positional offset such that it moves you towards your goal.

Review vector math, because if you have PositionA and PositionB as Vector3 objects, then the direction from A to B can be derived from

`````` Vector3 directionToMove = PositionA - PositionB;
``````

is this code a valid Ai
using UnityEngine;
using System.Collections;

public class enemy_AI : MonoBehaviour {
{

public transform target;
private float enemy_speed = 1f;
private float target_xpos;
private float target_ypos;
int dis_y;
int dis_x;

void start() {

target = GameObject.Find(“player”);

}

void Update() {

target.position.x == target_xpos;
target.position.y == target_ypos;
transform.position.x - target_xpos == dis_x;
transform.position.y - target_ypos == dis_y;

if(dis_x > 1) {
transform.translate(Vector3.right * enemy_speed * Time.deltaTime);
}

if(dis_y > 1) {
transform.translate(Vector3.up * enemy_speed * Time.deltaTime);
}

if(dis_x < -1) {
transform.translate(-Vector3.right * enemy_speed * Time.deltaTime);
}

if(dis_y < -1) {
transform.translate(-Vector3.up * enemy_speed * Time.deltaTime);
}

}
}

The general answer is yes and no. There are a number of logic and syntax errors you have in there. You should try to use MonoDevelop to help you code correctly.

Also… please make sure you use Code tags when posting code here… [ code][ /code]

``````using UnityEngine;
using System.Collections;

public class enemy_AI : MonoBehaviour {
{
public Transform target;
public float speed;

void Update(){
// first, get the direction which is (target - current)
Vector3 direction = target.position - transform.position;
// normalizing makes the length of that vector 1... or (10, 0, 0) is now (1, 0, 0)
Vector3 normal = direction.normalized;

// now translate the ai towards the target
// NOTE: Translate like I have it is world based
// using Translate as Self, means you would have to convert the direction to normal space
transform.Translate(normal * speed * Time.deltaTime, Space.World);
}
}
``````

in that it doesn’t employ any of the AI disciplines (search, learning, decision making etc.), no not really. If you’re searching for “AI” it tends to get complicated quickly, you don’t appear to be looking for that level of complexity yet.

It’s more a “controller”, it just makes something happen.

You might want to have a look at the tutorials in the learn section, perhaps: http://unity3d.com/learn/tutorials/projects/survival-shooter-project