Question by
MrBlue95 · Sep 12, 2019 at 10:57 PM ·
scripting problem
Random Movement Vector3 Not updating
Can someone tell me why this code upload the moveSpot.position only once after the start? Do you have any advices on how to solve it? Thank you in advance.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class Patrol : MonoBehaviour { private Transform moveSpot; public float speed; private float waitTime; public float startWaitTime;
public float minX;
public float maxX;
public float minZ;
public float maxZ;
void Start()
{
waitTime = startWaitTime;
GameObject Destination = new GameObject();
moveSpot = Destination.GetComponent<Transform>();
moveSpot.position = new Vector3(Random.Range(minX, maxX), 0.9999925f, Random.Range(minZ, maxZ));
Debug.Log(moveSpot.position);
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, moveSpot.position) < 0.3f)
{
if (waitTime <= 0)
{
moveSpot.position = new Vector3(Random.Range(minZ, maxZ), 0.9999925f, Random.Range(minZ, maxZ));
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
Comment