- Home /
AI teleporting
I made a ai script for the ai to randomly patrol but the ai is randomly teleporting. Help please!
My script for the patrolling:
float movespeed;
void Start()
{
movespeed = 1;
}
void Update()
{
var aix = Random.Range(0, 20);
var aiz = Random.Range(0, 20);
if (PlayerSeen == false)
{
transform.position = new Vector3(aix, 0.0f, aiz) * (movespeed);
}
}
Video of what ai is doing(sorry for bad quality):
https://www.mediafire.com/?54w2fnoc78yqr41
Thx
Comment
i didn't provide the other parts of the script like the playerseen variable. But idt it is important
That is expected behaviour with that code. Try lerping the position for a start and try reading some more basic tutorials.
Answer by Paricus · Dec 05, 2016 at 10:14 AM
you're going to want to use a moveTowards function, but its not a good way to do it because you're changing the new position every frame. trying changing it every so often with a timer
float movespeed;
float timer = 10;
void Start()
{
movespeed = 1;
}
void Update()
{
timer -= time.deltaTime
if(time<0)
{
var aix = Random.Range(0, 20);
var aiz = Random.Range(0, 20);
timer = 10;
}
if (PlayerSeen == false)
{
transform.position = Vector3.MoveTowards(transform.position, new Vector3(aix, 0.0f, aiz), movespeed);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102039im_/https://answers.unity.com/themes/thub/images/avi.jpg)