- Home /
How to wander and avoid sharp turns
Hello
I would like to make a wandering script, that avoids sharp turns
private void NextPoint()
{
Transform localTransform = transform;
//distance check, if we are close enough to the target, get a new one
while (Vector3.Distance(localTransform.position, target) < 2000) {
target = new Vector3 (UnityEngine.Random.Range (-5000, 5000), 0, UnityEngine.Random.Range (-5000, 5000));
}
localTransform.position = Vector3.RotateTowards (localTransform.position, target, 0.2f, 0.2f);
localTransform.position = Vector3.MoveTowards (localTransform.position, target, 200);
}
This does not handle sharp turns.
So this is what I have:
image:[2]
This is what I want:
image:[1]
Any ideas how to fix this?
[1]: /storage/temp/26967-untitled.png
[2]: /storage/temp/26966-untitled.png
Rotate with a Slerp ins$$anonymous$$d of using RotateTowards
You can rotate object each frame depending on a difference between it's current forward direction and direction to the target. That could look quite smooth and nice and will give an impression of some inertia.
Answer by frogsbo · May 27, 2014 at 03:03 PM
as meat5000 said. and also you can use Mathf.SmoothDamp to smooth the change in between target points so that they take required time to change from one point to other, still using rotate towards.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150016im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector3 Clamp 3 Answers
How precise is unity with it´s transforms? 2 Answers
Angle between player and rocket targetting player. 1 Answer
Vector3 Lerp mis-match 0 Answers
How can I Vector3 Slerp or Lerp locally? 3 Answers