- Home /
Nav Agent multiple Units problem
Hello everyone.
I got a problem with Nav Agent in Unity 5. Everythink works perfectly well for 4-5 units, but when i select more they act like crazy. I tried to debug and I am sure that everyone gets good target. Don't know if it is important, but this is mobile game.
My navigation script: (I'm also creating animation in target place, that is why GameObject is made there) using UnityEngine; using System.Collections; using System;
public class TouchNavigation : Interaction { private GameObject clickObj; public GameObject Click; public Transform location;
public float RelaxDistance = 10;
private NavMeshAgent agent;
private Vector3 target = Vector3.zero;
private bool selected = false;
private bool isActive = false;
private Vector3 change = Vector3.zero;
private Vector3 firstTarget = Vector3.zero;
public Vector3 RotateV = Vector3.zero;
private bool rotateDone = false;
private bool isCreated = false;
private float timesincestart = 0;
public override void Dselect()
{
selected = false;
}
public override void Select()
{
selected = true;
}
public void sendToTarget()
{
Debug.Log("Called, target is: " + target);
agent.SetDestination(target);
agent.Resume();
isActive = true;
}
private bool closeto(Vector3 start, Vector3 finish, Vector3 change)
{
if ((finish.x > start.x - change.x && finish.x < start.x + change.x) && (finish.y > start.y - change.y && finish.y < start.y + change.y) && (finish.z > start.z - change.z && finish.z < start.z + change.z))
{
return true;
}
return false;
}
void Start()
{
clickObj = null;
agent = GetComponent<NavMeshAgent>();
change.x = 20;
change.y = 20;
change.z = 20;
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began && selected)
{
timesincestart = Time.time;
firstTarget = RtsManager.Current.ScreenPointToMapPosition(Input.GetTouch(0).position);
if (firstTarget == Vector3.zero)
return;
var ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
Physics.Raycast(ray, out hit);
var interact = hit.transform.GetComponent<Interactive>();
if (interact != null)
return;
Vector3 clickSpot = firstTarget;
clickSpot.y = 10;
if (isCreated == false)
{
clickObj = (GameObject)GameObject.Instantiate(Click, clickSpot, location.rotation);
rotateDone = false;
isCreated = true;
}
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (isCreated)
{
Destroy(clickObj);
isCreated = false;
rotateDone = true;
return;
}
}
}
if (selected == false && isCreated == true)
{
Destroy(clickObj);
isCreated = false;
rotateDone = true;
return;
}
if (selected && Input.touchCount != 0)
{
var tempTarget = RtsManager.Current.ScreenPointToMapPosition(Input.GetTouch(0).position);
if (tempTarget != Vector3.zero)
{
if (rotateDone == false)
{
if (clickObj != null)
{
clickObj.transform.Rotate(RotateV * Time.deltaTime * 4);
clickObj.transform.position = tempTarget;
}
}
if (!(closeto(firstTarget, tempTarget, change)))
{
Destroy(clickObj);
rotateDone = true;
isCreated = false;
return;
}
float timenow = Time.time;
if (timenow - timesincestart >= 1)
{
rotateDone = true;
if (closeto(firstTarget, tempTarget, change))
{
target = tempTarget;
sendToTarget();
}
else
{
Destroy(clickObj);
rotateDone = true;
isCreated = false;
return;
}
}
}
}
if (isActive && Vector3.Distance(target, transform.position) < RelaxDistance)
{
agent.Stop();
isActive = false;
}
}
}
Your answer
Follow this Question
Related Questions
How to queue NavMeshAgents on entering a tile in Unity (based on path distance to tile)? 0 Answers
NavMesh Agent rotation problem 3 Answers
Making NavMesh areas? 0 Answers
Why Nav Mesh Agent has been baked intermittently on 1 straight component? 0 Answers
How to generate Nav Mesh floor in a procedural generated maze? 3 Answers