The main goal of this game’s script is to have planes move around an airport then take off once they reach a certain point. I have the planes as NavMeshAgents along the airport. This is the code that controls the movement. The planes move from point to point without problems, but whenever one of the planes reach the point where they need to take off, Unity freezes, and I need to go through the task manager to close it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneScript : MonoBehaviour {
//destinations
public Transform a;
public Transform b;
public Transform c;
public Transform d;
public Transform e;
//has plane reached runway takeoff point
private bool nyes = false;
public float forwardSpeed = 5f;
public float upSpeed = 1f;
//densitation marks the current changing destination
public Transform densitation;
void OnTriggerEnter(Collider other)
{
UnityEngine.AI.NavMeshAgent agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
agent.destination = densitation.position;
//change destination to next point when current point is reached
if (other.gameObject.tag == "A")
{
densitation = b;
agent.destination = densitation.position;
}
if (other.gameObject.tag == "B")
{
densitation = c;
agent.destination = densitation.position;
}
if (other.gameObject.tag == "C")
{
densitation = d;
agent.destination = densitation.position;
}
if (other.gameObject.tag == "Runway")
{
densitation = e;
agent.destination = densitation.position;
}
if (other.gameObject.tag == "Takeoff")
{
nyes = true;
}
}
//takeoff from runway
void Update () {
while (nyes == true)
{
transform.Translate(Vector3.forward * forwardSpeed * Time.deltaTime);
transform.Translate(Vector3.up * upSpeed * Time.deltaTime);
}
}
}
Is the problem that the planes translate themselves then disconnect from the navigation mesh? If so, is there a way to get rid of the connection before it moves to prevent the crash?
Thanks in advance!