hey guys, Need an IA script to make an enemy chase a target please !!

i already made this one :
using UnityEngine;
using System.Collections;

public class EnnemiScript : MonoBehaviour {
public float m_dov;
public float m_speed = 2f;
public GameObject dalila;
// Use this for initialization
void Start () {

}

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

GameObject cible = null;
Collider[ ] colliders = Physics.OverlapSphere(transform.position, m_dov);
if (colliders.Length > 1)
{

foreach (Collider ColliderCurrent in colliders)
{
if (ColliderCurrent.gameObject != gameObject)
{

cible = ColliderCurrent.gameObject;
print(“bonjour”);
Debug.DrawLine(transform.position, ColliderCurrent.transform.position);
}
}
}
//dalila.transform.position=Vector3(cible.transform.position.x-1,cible.transform.position.y-1,cible.transform.position.z-1);
Vector3 v2cible = cible.transform.position - transform.position;
//Vector3 v2=transform.Translate(-1f,-1f,0);
print(cible.transform.position.x);

v2cible.Normalize();

v2cible *= m_speed;
v2cible *= Time.deltaTime;
transform.Translate(v2cible);

}

}

its works but it seems to have something wrong cause the follower takes exactly the same position as the target and stuck to it, and me, i want that the follower to stop close to the target but not in the same position as it.
so i tried this one :

var target : Transform;

function Update(){
var right = transform.TransformDirection(Vector3.right);
var left = transform.TransformDirection(Vector3.left);
var fwd = transform.TransformDirection(Vector3.forward);

if (!Physics.Raycast(transform.position, fwd, 20) !Physics.Raycast(transform.position, right, 40) !Physics.Raycast(transform.position, left, 40)){

transform.Translate(Vector3.zero);
transform.LookAt(target);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, fwd, 20)) {
transform.Translate(Vector3.zero);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, right, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
else if (!Physics.Raycast(transform.position, left, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
}
}

this one too works but the follower do the job lying down on the belly so … :frowning: please help me and if you can correct the mistakes or suggest me an other script it will be very nice of you thanks :-*

For the first one I think you need to set up a maxDistance that targets the players transform.

void Start () {
GameObject go = GameObject.FindGameObjectWithTag(“Player”);

target = go.transform;

maxDistance = 35;

}

You should use the Code tags to wrap your code so it displays cleaner on here and can be read.

In the meantime, I posted this the other day if you want it. Given, it’s written in c# and not unityscript.
It’ll do everything you want and more :wink:

http://forum.unity3d.com/threads/89543-Free-Simple-AI-Behavioral-script

Regards,
Rob

Typically this is solved by the navigation system, through the notion of an “arrival distance”. You create a navigation request that specifies how close the agent should get to his target, and the agent will travel toward his target until he is within X meters, where X is the arrival distance.

I just released a software package on the asset store that includes a chase behavior (go to 1:40 in this video). The package has a parameter called “arrival distance” that you can modify in the Inspector window.

But, if you just want really really simple behavior (and there are no other obstacles in the scene), then you dont need pathfinding.

thx everyone you’ve helped me much :slight_smile: