My "teammate" is using NavMeshAgent to follow player. In unity 5.3.2f1(newest) i can set "Stopping distance" but there is no option to set Keep distance (what should be implemented).
The way my teammate is kepping distance is bad cause he is just teleporting back (maybe that's the issue) but i think no matter what i put there it will freeze:
http://i.imgur.com/7SHLx5o.gif
How to make this to not freeze whole unity?
if (distance < 2)
{
Debug.Log(distance);
distance = 2;
transform.position = (transform.position - target.transform.position).normalized * distance + target.transform.position;
}
Full code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof (NavMeshAgent))]
public class Teammate : MonoBehaviour {
NavMeshAgent pathfinder;
Transform target;
float distance = 0;
void Start () {
pathfinder = GetComponent ();
target = GameObject.FindGameObjectWithTag ("Player").transform;
StartCoroutine(UpdatePath());
}
void Update () {
}
IEnumerator UpdatePath()
{
float refreshRate = .25f;
while (target != null)
{
distance = Vector3.Distance(this.transform.position, target.transform.position);
if (distance < 2)
{
Debug.Log(distance);
distance = 2;
transform.position = (transform.position - target.transform.position).normalized * distance + target.transform.position;
}
else {
Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
pathfinder.SetDestination(targetPosition);
yield return new WaitForSeconds(refreshRate);
}
}
}
}
↧