Student new to coding so sorry if my code doesn’t make much sense!
In my set up I currently have a giant cube that has this script(JS) attached to it:
public var prefab: Transform;
function Update () {
if(Input.GetMouseButtonDown(0)){
Instantiate(prefab, new Vector3(0, 0, 3), Quaternion.identity);
}
}
So when clicked a sphere is spawned and dropped onto a plane below. When spawned I want these spheres to move to a random coordinate along the plane, then once it gets there to move to another random point. Here is what I have:
var speed: float;
var arrived: boolean;
var randX: float;
var randZ: float;
var newPosition;
function Start(){
arrived = false;
randX = Random.Range(-5, 5);
randZ = Random.Range(-5, 5);
newPosition = new Vector3(randX, -1.504, randZ);
}
function Update () {
if(!arrived) {
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * speed);
if(GameObject.Find('SpawnedObj').transform.position.x == randX) {
arrived = true;
randX = Random.Range(-5, 5);
randZ = Random.Range(-5, 5);
newPosition = new Vector3(randX, -1.504, randZ);
};
};
else if(arrived){
// WaitForSeconds(1);
arrived = false;
};
}
As you can probably tell when I run this the sphere moves to the initial random coordinate, but then doesn’t move after that. How can I fix this? Also I want to have it so that when it reaches the coordinate it waits a few seconds before moving to the next, but I don’t know how to do that. I dabbled with WaitForSeconds, but if thats the right way to go I don’t think I used it correctly. Any kind of help would be greatly appreciated!
UPDATE:
So as I was messing around with it some more on my own I changed the GameObject.Find(‘SpawnedObj’).transform.position.x to just transform.position.x and then changed speed from 1 to 50. What I noticed is it would then move from point to point, but at seemingly random intervals. When speed is changed back to 1 it will not move from its initial random point. I have no idea why this is. Help in any form will still be appreciated!