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…
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:
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);
}
}
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