Having some problem with AI finding a vector3...
Okay so this script works pretty good the only problem is my ally will not find their StartPos. take a look, basicly the state that tells them that they are home is not activated as they cannot find the correct transform... Please just read the script tell me what you think and i should do... And why myTransform does not equal StartPos when they are at the startPOS.
using UnityEngine; using UnityEngine.AI;
public class AllyAI : MonoBehaviour { //Transforms public Transform Target; public int rotSpeed; public int maxDistance;
private Transform myTransform;
private Vector3 NPCstartPos;
private Quaternion NPCstartRot;
//Essenial Compnents
private Animator anim;
//NavMesh Components
Transform _destination;
NavMeshAgent _nvAgent;
//States
public bool NPC1IdleState;
public bool NPC2HomeState;
public bool NPC3AttackState;
public bool NPC4ReturnHomeState;
void Awake() {
myTransform = transform;
NPCstartPos = transform.position;
NPCstartRot = transform.rotation;
//Set States
NPC1IdleState = true;
NPC2HomeState = true;
NPC3AttackState = false;
NPC4ReturnHomeState = false;
}
// Use this for initialization
void Start () {
maxDistance = 1;
//Get Components
anim = GetComponent<Animator>();
_nvAgent = this.GetComponent<NavMeshAgent>();
//Set NavMeshFalse
gameObject.GetComponent<NavMeshAgent>().enabled = false;
}
// Update is called once per frame
void Update () {
//DEBUG
Debug.DrawLine(Target.position, myTransform.position, Color.cyan);
Vector3 direction = Target.position - this.transform.position;
//Attack State
if (Vector3.Distance(Target.position, myTransform.position) > maxDistance && Vector3.Distance(Target.position, myTransform.position) < 20)
{
//Look at Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(Target.position - myTransform.position), rotSpeed * Time.deltaTime);
//Enable NavMesh
gameObject.GetComponent<NavMeshAgent>().enabled = true;
//Move to Target
anim.SetTrigger("isWalking");
_destination = Target;
Vector3 targetVector = _destination.transform.position;
_nvAgent.SetDestination(targetVector);
//Set State
NPC1IdleState = false;
NPC2HomeState = false;
NPC3AttackState = true;
NPC4ReturnHomeState = false;
}
//Idle State
else if (Vector3.Distance(Target.position, myTransform.position) < maxDistance) {
anim.SetTrigger("isIdle");
_destination = null;
gameObject.GetComponent<NavMeshAgent>().enabled = false;
//Set State
NPC1IdleState = true;
NPC2HomeState = false;
NPC3AttackState = false;
NPC4ReturnHomeState = false;
}
//Target Out Of Sight
if (Vector3.Distance(Target.position, myTransform.position) > 20)
{
//Check To Return Home
if (myTransform.position != NPCstartPos && myTransform.rotation != NPCstartRot)
{ //Set States
NPC1IdleState = false;
NPC2HomeState = false;
NPC3AttackState = false;
NPC4ReturnHomeState = true;
//DEBUG
Debug.Log("Ally Cannot Find Home");
if (myTransform.position == NPCstartPos && myTransform.rotation == NPCstartRot)
{
//Set States
NPC1IdleState = false;
NPC2HomeState = true;
NPC3AttackState = false;
NPC4ReturnHomeState = false;
//DEBUG
Debug.Log("Ally Found Home");
}
}
}
//Return To Home State
if (NPC4ReturnHomeState == true)
{
//Enable NavMesh
gameObject.GetComponent<NavMeshAgent>().enabled = true;
//Set NavMesh Variables
_destination.position = NPCstartPos;
Vector3 targetVector = _destination.position;
_nvAgent.SetDestination(targetVector);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(_destination.position - myTransform.position), rotSpeed * Time.deltaTime);
//Set Animation
anim.SetTrigger("isWalking");
}
//At Home
else if (NPC2HomeState == true)
{
_destination = null;
//Disable NavMesh
gameObject.GetComponent<NavMeshAgent>().enabled = false;
//Set Animation
anim.SetTrigger("isIdle");
//Set States
NPC1IdleState = true;
NPC2HomeState = true;
NPC3AttackState = false;
NPC4ReturnHomeState = false;
}
}
}
Comment