So i am trying to make a enemy ai that if it sees the player goes to it and if the player gets out of the sight of the enemy, it wil go to the last know position of the player. I got it set up the part where it saves the last know position of the player but i need a way to check if the enemy has visited the last know posistion, but i dont realy know how to do it.
using UnityEngine;
using System.Collections;
public class EnemySight : MonoBehaviour
{
public float InRange = 100f;
public float FOVAngle = 50f;
public float Movespeed = 5f;
public float Rotatepeed = 5f;
public float minDist = 2f;
public Transform target;
private Quaternion _lookRot;
public bool PlayerInSight;
public bool HasVisitedLastPosition = false;
public Transform StartPosition;
public Vector3 personalLastSighting;
public Vector3 position = new Vector3(1000f, 1000f, 1000f);
public Vector3 resetPosition = new Vector3(1000f, 1000f, 1000f);
// Update is called once per frame
void Start()
{
}
void Update ()
{
Vector3 StartPoint = StartPosition.position - transform.position;
PlayerSeen ();
if (PlayerInSight == false && HasVisitedLastPosition == false) // is the player beeing seen?
{
transform.position = Vector3.MoveTowards(transform.position, personalLastSighting, Movespeed * Time.deltaTime);
HasVisitedLastPosition = true;
}
if(HasVisitedLastPosition == true && PlayerInSight == false)
{
transform.position = Vector3.MoveTowards(transform.position, StartPoint, Movespeed * Time.deltaTime); //if yes, go to StartPoint
}
}
void PlayerSeen()
{
Vector3 StartPoint = StartPosition.position;
float Distance = Vector3.Distance (target.transform.position, gameObject.transform.position);
Vector3 TargetDir = target.position - transform.position;
Vector3 forward = transform.forward;
float Angle = Vector3.Angle (TargetDir, forward);
position = GameObject.FindGameObjectWithTag("Player").transform.position;
RaycastHit hit;
if (Distance < InRange) //is player in viewing range?
{
target.GetComponent<Renderer> ().material.color = Color.green;
if (Angle < FOVAngle) //is player in viewing angle?
{
target.GetComponent<Renderer> ().material.color = Color.red;
if (Physics.Raycast (transform.position, TargetDir,out hit, InRange) ) //is there an object between the player and the enemy?
{
if(hit.transform.gameObject.tag == "Player") // if so do ...
{
Debug.DrawRay(transform.position, TargetDir, Color.green);
target.GetComponent<Renderer> ().material.color = Color.cyan;
PlayerInSight = true;
personalLastSighting = position;
if (Distance > minDist)
{
transform.LookAt(target);
transform.position = Vector3.MoveTowards(transform.position, target.position, Movespeed * Time.deltaTime); //looks at target and goes to it
position = transform.position;
}
}
else
{
PlayerInSight = false;
}
}
}
}
}
}