Return spawn point when out of range

Hi guys i hv this beginig ai script and it works perfectly i just wonder how can i change it to when i go out out enemy range it return to initial position and not stay in position when it stop follow me…

thats the script…

ty

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour
{
    float distance;
    float lookAtDistance = 25.0f;
    float attackRange = 15.0f;
    float moveSpeed = 5.0f;
    float damping = 6.0f;

    public Transform target;
   
    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);
       
        if(distance < lookAtDistance)
        {
            renderer.material.color = Color.yellow;
            LookAt();
        }
       
        if(distance > lookAtDistance)
        {
            renderer.material.color = Color.green;
        }
       
        if (distance < attackRange)
        {
            renderer.material.color = Color.red;
            AttackPlayer();
        }
    }
   
    void LookAt()
    {
        Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }
   
    void AttackPlayer()
    {
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }
}

if helps the explanation i want to know how do i make to back to initial position when it turn yellow to green…

Something like this:

    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);
     
        if(distance < lookAtDistance)
        {
            renderer.material.color = Color.yellow;
            LookAt();
        }
     
        if(distance > lookAtDistance)
        {
            if(renderer.material.color == Color.Yellow) {
                 ReturnToInitialPosition();
            }
            renderer.material.color = Color.green;
        }
     
        if (distance < attackRange)
        {
            renderer.material.color = Color.red;
            AttackPlayer();
        }
    }

But instead of colors you should probably use an enum instead :'P

The colors was only to test i already change to a game charater :slight_smile: i will test this… Ty for tje support

Ok i test but i think i dont get it right. Can u plz help me with that… my corrent code is that and im not figure in out how can i turn the enemy back to inicial position when player go out of his attackRange…

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour
{
    float distance;
    float lookAtDistance = 25.0f;
    float attackRange = 15.0f;
    float damping = 6.0f;

    public int speed;

    public AnimationClip idle;
    public AnimationClip run;

    public Transform target;
   
    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);
       
        if (distance < lookAtDistance)
        {
            animation.CrossFade (idle.name);
            LookAt ();
        }
        else
        {
               
        }
       
        if (distance < attackRange)
        {
            animation.CrossFade(run.name);
            AttackPlayer();
        }

    }
   
    void LookAt()
    {
        Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }
   
    void AttackPlayer()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
}

Create a boolean called “isReturning”.
now in the update function you’ll run the usual code if he isn’t returning, BUT if u want it to return, set that boolean to true. And so you’ll make the AI move to the initial position. when the player reaches the initial position you can change the boolean to false again. u can store the initial position in the start function:

private Vector3 initialPosition;

void Start() {
     initialPosition = transform.position;
}

Hi Magichiian thx for ur help. I kinda understand wt u are trying to teach me but i cant put them to work i try the code above but seems something is missing me and the enemy dont return to initial position… can u plz give a hand again :s
appreciate ut help.

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour
{
    float distance;
    float lookAtDistance = 25.0f;
    float attackRange = 15.0f;
    float damping = 6.0f;

    public int speed;

    bool isReturning;

    public AnimationClip idle;
    public AnimationClip run;

    public Transform target;

    private Vector3 initialPosition;

    void start()
    {
        initialPosition = transform.position;
        isReturning = false;
    }
   
    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);
       
        if (distance < lookAtDistance)
        {
            animation.CrossFade (idle.name);
            LookAt ();
        }
        else
        {

        }

        if (distance > lookAtDistance && distance>attackRange)
        {
            isReturning = true;
        }
               
        if (distance < attackRange)
        {
            animation.CrossFade(run.name);
            AttackPlayer();
        }

    }
   
    void LookAt()
    {
        Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }
   
    void AttackPlayer()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
}
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
    float distance;
    float lookAtDistance = 25.0f;
    float attackRange = 15.0f;
    float damping = 6.0f;
    public int speed;
    bool isReturning;
    public AnimationClip idle;
    public AnimationClip run;
    public Transform target;
    private Vector3 initialPosition;

    void start()
    {
        initialPosition = transform.position;
        isReturning = false;
    }
    void Update()
    {
        if (!isReturning)
        {
            distance = Vector3.Distance(target.position, transform.position);
         
            if (distance < lookAtDistance)
            {
                animation.CrossFade (idle.name);
                LookAt (target.position);
            }
            else //LookDistance is always bigger than the attackRange so i commented it out. //if (distance > lookAtDistance) && distance>attackRange)
            {
                isReturning = true;
            }
                 
            if (distance < attackRange)
            {
                animation.CrossFade(run.name);
                MovePlayerForward();
            }
        }
        else 
        {
            LookAt (initialPosition);
            MovePlayerForward();
           
            distance = Vector3.Distance(initialPosition, transform.position);
            if (distance < 20)
            {
                isReturning = false;
            }
        }
    }
    void LookAt(Vector3 targetPosition)
    {
        Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }
   
    void MovePlayerForward()
    {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
}

Create a variable of type Vector3 called OriginalPosition, at Start() set it to this.Transform.Position;

Later on say Transform.Position = OriginalPosition

@Paykoman
By the way, why aren’t you using navmesh to make it follow the player?

@RJ-MacReady
I think he wanted it to walk back, not teleport o.o

Yes i want it to run back to initial position… Good point i forget the navmesh but i need learn use it propertly… So can u plz tell me wt im doing wrong with this method? i will try later the navmesh one… Appreciate all the help

Nvm u al

ready answer me :slight_smile:

1 Like

Same thing, except set target position to OriginalPosition.

There is no target position though :'P

Well maybe that’s your problem.

Mostly the problem of @Paykoman , since he has a very limited knowledge on programming.

We all have very limited knowledge of programming. This is not understanding the problem.

Instead of arguing with me, how about you give him a bit more elaborate advice on how to achieve the AI to walk back…
xD

:slight_smile: y lol i appreciate if