- Home /
Monster in a certain range of random mobile problems
My English is not good,The problem description may have unclear place ,Hope someone to guide it To achieve the function is a monster in the range of random movement, but the monster to a new position every time not to move from the original position, but suddenly changed to an inexplicable position and then began to move to the new location ,I used DOTween. patrol() Is a function of movement,patrolpoint() Returns a random location,The code is as follows
void Start () {
StartCoroutine (Patrol ());
}
void Update () {
StartCoroutine (_Patrol ());
}
IEnumerator _Patrol(){
yield return new WaitUntil (() => transform.position == next);
last = patrolPoint ();
patrol (last);
yield return new WaitUntil (() => transform.position == last);
next = patrolPoint ();
patrol (next);
}
IEnumerator Patrol(){
last = patrolPoint ();
patrol (last);
yield return new WaitUntil (() => transform.position == last);
next = patrolPoint ();
patrol (next);
}
public void patrol(Vector3 vec){
transform.DOLookAt (vec, 2);
//transform.position = Vector3.Lerp (transform.position, vec, 0.5);
transform.DOMove (vec, 5);
}
public Vector3 patrolPoint(){
Vector3 MovePoint;
Vector3 OriginPoint;
OriginPoint = new Vector3 (-27f, transform.position.y, -30f);
MovePoint = new Vector3 (Random.Range (OriginPoint.x - 25, OriginPoint.x + 25),
transform.position.y, Random.Range (OriginPoint.z - 40, OriginPoint.z + 40));
return MovePoint;
}
Answer by henry_jvx · Jun 14, 2017 at 07:09 AM
you are calling yielded coroutine through update loop. it causes engine remove yield calls and execute the whole coroutine in a single frame.
i think you should create your own coroutine loop but i higly suggest you use pathfinding system for cpu player moviments.
Your answer
![](https://koobas.hobune.stream/wayback/20220612124134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
the object passes through the collider 1 Answer
Dense joid joining! Help 0 Answers
See camera point of view ingame 1 Answer
Offscreen object movement gets wrong 0 Answers
How to make the same scaling of sub-projects during export? 0 Answers