- Home /
How to Update a GameObject coordinates to folow in realtime and don't lose performens or crushing Unity.[SOLVED]
I have a small issue and jus out of ideas how to do it, I want to update a GameObject
Vector3
in realtime but if I use simple Update
method fps drops low and it's understandable.
Ther is a many objects whit tag enemy in the scene and I want to make a main object to folow the closest and change the object to dolow if another "enemy" object became closer.
I also tryed to use System.Threading
but it did't give me ane result fps lowers the same way.
Here is small excample of what I'am trying to achive:
Vector3 target;
private void Start()
{
StartCoroutine(UpdatePath());
}
private IEnumerator UpdatePath()
{
target = GameObject.FindGameObjectWithTag("Enemy").transform.position;
StartCoroutine(FolowPath());
yield return null;
}
private IEnumerator FolowPath()
{
while (true)
{
Vector3.MoveTowards(transform.position, target, 20 * Time.deltaTime);
StartCoroutine(UpdatePath());
yield return null;
}
}
Answer by IgorAherne · Mar 10, 2017 at 06:01 PM
Oh god,
UpdatePath starts FollowPath1,
FollowPath1 starts UpdatePath wich launches FolowPath2,
FollowPath1 continues its while loop, starts FollowPath3,
FollowPath2 launche UpdatePath, which launches FollowPath4,
Follow Path3 launches UpdatePath which launches FollowPath5,
Follow path5 launches UpdatePath which launches FollowPath6, etc
Soon you explode with an insane amount of FollowPaths calling their operations from never ending while loops, evolving in a geometric progression.
Do this instead:
private void Start()
{
StartCoroutine(UpdateAndFollowPath());
}
private IEnumerator UpdateAndFollowPath()
{
while (true)
{
//move this like into here:
target = GameObject.FindGameObjectWithTag("Enemy").transform.position;
Vector3.MoveTowards(transform.position, target, 20 * Time.deltaTime);
//UpdatePath()); remove this
yield return null;
}
}
I guess it my falt a multiple corotines is bad thing your advice increases fps, not perfect but better thanks.
$$anonymous$$ultiple coroutines are not a bad thing. Infinitely recursive coroutines are, however, a (very) bad thing. Another bad thing is calling GameObject.FindGameObjectWithTag every frame. Find your enemy once on Start and cache the reference ins$$anonymous$$d:
private Transform enemyTransform;
private void Start()
{
enemyTransform = GameObject.FindGameObjectWithTag("Enemy").transform;
StartCoroutine(UpdateAndFollowPath());
}
private IEnumerator UpdateAndFollowPath()
{
while (true)
{
//move this like into here:
target = enemyTransform.position;
Vector3.$$anonymous$$oveTowards(transform.position, target, 20 * Time.deltaTime);
//UpdatePath()); remove this
yield return null;
}
}
I have many "clones" of the same object "enemy" that I Instantiated that why I have to use GameObject.FindGameObjectWithTag
and I need always update the position of clones to find the closest one because they are always moving. and If I use main pref it will cause the modifying of all clones at ones.
$$anonymous$$aybe there is a better whay of doing this but I don't know how for now.
You can cache it "intelligent" in that case:
Transform enemyTransform;
private IEnumerator UpdateAndFollowPath()
{
while (true)
{
if (enemyTransform == null)
enemyTransform = GameObject.FindGameObjectWithTag("Enemy").transform;
// [ ... ]
If you currently don't have an enemy, search for it. If it got destroyed it will search for a new one. In between it will reused the one stored in enemyTransform.
edit
Btw: GameObject.FindGameObjectWithTag
will not return the closest one but simply any object that is tagged with that tag. Usually in the order they have been created. If you want to target the closest enemy you probably want to use Physics.OverlapSphere to get all objects around you in a certain radius.
If you have many enemies and it should target the closest everywhere in the scene you might want to use an "Enemy$$anonymous$$anager" where each enemy registers itself on creation and removes itself on destroy.
Your answer
Follow this Question
Related Questions
Code working on wrong axis. 1 Answer
Starting a Coroutine in Update acting differently in play mode and built game 1 Answer
Distribute terrain in zones 3 Answers
Using multiple yields in a Coroutine 1 Answer
Multiple Cars not working 1 Answer