Question by
MrBlue95 · Sep 10, 2019 at 12:37 PM ·
scripting problemmovementrandomrandom.range
Different random number - Same script - Different cloned object
I would like to obtain a random movement of the objects which are spawned at the Start. This code makes all the objects move towards the same destination. Any ideas of how to fix this without rewriting the entire movement script?? Thank you in advance.
public class Patrol : MonoBehaviour {
public float speed;
private float waitTime;
public float startWaitTime;
private Transform moveSpot;
public float minX;
public float maxX;
public float minZ;
public float maxZ;
private GameObject PlatformCenter;
void Start()
{
PlatformCenter = GameObject.FindGameObjectWithTag("PlatformCenter");
waitTime = startWaitTime;
moveSpot = PlatformCenter.GetComponent<Transform>();
moveSpot.position = new Vector3(Random.Range(minX, maxX), 1.083333f, Random.Range(minZ, maxZ));
}
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, moveSpot.position) < 0.2f)
{
if (waitTime <= 0)
{
moveSpot.position = new Vector3(Random.Range(minX, maxX), 1.083333f, Random.Range(minZ, maxZ));
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to generate a random color? 5 Answers
Create random numbers of object and move them 0 Answers
WSAD Movement performs unwanted flipping of character 0 Answers
Stop movement on collision 1 Answer