I want my code to tell the AI to move from one log then back to the fire chamber and then to the other log but it just moves to the first log and doesnt do anything did i code something wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WoodmanAI : MonoBehaviour { public GameObject log1; public GameObject log2; public GameObject log3; public GameObject log4; public GameObject log5; public GameObject Firechamber;
private UnityEngine.AI.NavMeshAgent navAgent;
public GameObject agentDestination;
public void OnTriggerEnter(Collider other)
{
//Once Player collides with this log they destroy the prefab
if (other.tag == "Log1")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab and return to the fire place
if (other.tag == "Log2")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab and return to the fire place
if (other.tag == "Log3")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab and return to the fire place
if (other.tag == "Log4")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab
if (other.tag == "Log5")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Return to fireplace after collecting log
if (other.tag == "Fireplace")
{
if (GameObject.Find("Log2") != null)
{
navAgent.destination = log2.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log3") != null)
{
navAgent.destination = log3.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log4") != null)
{
navAgent.destination = log4.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log5") != null)
{
navAgent.destination = log5.transform.position;
other.gameObject.SetActive(false);
}
}
}
// Start is called before the first frame update
void Start()
{
navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
if (agentDestination != null)
{
navAgent.destination = log1.transform.position;
}
}
}
here is more of the code public GameObject log2; public GameObject log3; public GameObject log4; public GameObject log5; public GameObject Firechamber;
private UnityEngine.AI.Nav$$anonymous$$eshAgent navAgent;
public GameObject agentDestination;
public void OnTriggerEnter(Collider other)
{
//Once Player collides with this log they destroy the prefab
if (other.tag == "Log1")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
public GameObject log1; } //Once Player collides with this log they destroy the prefab and return to the fire place if (other.tag == "Log2")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab and return to the fire place
if (other.tag == "Log3")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab and return to the fire place
if (other.tag == "Log4")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Once Player collides with this log they destroy the prefab
if (other.tag == "Log5")
{
other.gameObject.SetActive(false);
navAgent.destination = Firechamber.transform.position;
}
//Return to fireplace after collecting log
if (other.tag == "Fireplace")
{
if (GameObject.Find("Log2") != null)
{
navAgent.destination = log2.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log3") != null)
{
navAgent.destination = log3.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log4") != null)
{
navAgent.destination = log4.transform.position;
other.gameObject.SetActive(false);
}
if (GameObject.Find("Log5") != null)
{
navAgent.destination = log5.transform.position;
other.gameObject.SetActive(false);
}
}
}
// Start is called before the first frame update
void Start()
{
navAgent = GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>();
}
// Update is called once per frame
void Update()
{
if (agentDestination != null)
{
navAgent.destination = log1.transform.position;
}
}
}
Answer by BradyIrv · Jun 08, 2019 at 03:49 PM
The first thing I can think of is that the AI doesn't have a rigidbody component. It will only register collision if one of the two object has a rigidbody and both objects have a collider.
Also, as an alternative to the way you have it coded, you could create a node system. Basically create a script for a node (log position), and that node knows it's next destination. It's a bit more simple because then you don't need collision or tags. You just check to see if your AI has reached the destination, query the log, and set the next based on the log data. If that helps I also have code for reaching the destination that I found and use from another post, I can post it again here if you'd like.
Answer by wesacretella2000 · Jun 09, 2019 at 04:53 AM
Yes please can you send the code? @BradyIrv
// This will return true when the agent reaches his destination
private bool AtDestination(Nav$$anonymous$$eshAgent agent) {
if (agent.pathPending)
{
if (!agent.isOnNav$$anonymous$$esh)
{
Debug.LogWarning("Agrent not on navmesh");
return false;
}
else if (agent.remainingDistance <= agent.stoppingDistance)
{
if (!agent.hasPath || agent.velocity.sqr$$anonymous$$agnitude == 0f)
{
return true;
}
else return false;
}
else return false;
}
else return false;
}