Ok, So I am having trouble with my EnemyAI script. The problem is is that I want the enemy to stop walking towards me after a certain amount of time and I have that set up in my script but it doesn’t work for some reason. Can you please show/tell me how to fix it, Thanks!!! (It is in C# by the way)
Here is the script,
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public float MoveSpeed = 5;
public Transform Player;
public int MaxDist = 45;
public int MinDist = 500;
private GameObject soldier;
// Use this for initialization
void Start ()
{
GetComponent<EnemyGun>();
soldier = GameObject.Find("Soldier");
}
// Update is called once per frame
void Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
soldier.animation.Play("RunAim");
}
else
{
soldier.animation.Play("StandingAim");
}
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
gameObject.GetComponent<EnemyGun>().Fire();
}
i might be able to help i found this pice of script that you might want to give a try
once you get in proximity of the enemy itl look at you and if you go closer itl charge at you but you can chaing this so as soon as it sees you itl run at you and when you get out of range itl stop chassing you
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
renderer.material.color = Color.red;
Your values for min and max distance are looking weird to me. minDist is 500 so your guy will stop walking towards the player at 500, but the fire is at less than 45. So if you don’t walk towards the enemy, he will never be close enough (45) to shoot at you.
I think your minDist is more likely to be 45 and maxDist 50, that would make more sense.