- Home /
Why does Unity 2017.3.0f3 crash when NavMeshAgent is scripted to transform.Translate
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!
Answer by adriant · Dec 27, 2017 at 10:11 AM
You can disable the NavMeshAgent any time time you want with a statement like this: agent.enabled = false;
This way the NavMeshAgent will not try to keep the airplane stuck to the NavMesh.
Thanks, I've been looking for a way to do that forever. I also found the reason of the freeze. Using while() in void Update().
Answer by CSXsonic · Jan 03, 2018 at 07:11 AM
I found why it was freezing. I used while() in void Update() lines 55 - 60.
Your answer
Follow this Question
Related Questions
Nav Mesh Agent para perseguir a un clon 1 Answer
Editing NavMesh Paths? 0 Answers
NavAgent not following on rebuilt NavMesh 0 Answers
NavMeshAgent Destination Problem 0 Answers