How do I make a cube teleport after dropping below a point?

Hi,
I’m working on a simple project,and I’m new to Unity. I want a cube to drop and the player must avoid it. Then, after the cube reaches Y<-4.5, I want it to teleport to Y: 5, and the X is to be a randomly generated value between -2 and 4. If someone could provide me with the code I’d need, that would be really great (:

Thanks in advance!

What about Z? My answer below preserves the current Z value.

1 Answer

1

To facilitate your cube raining extravaganza please apply the following script to each cube:

void FixedUpdate() {
    if (transform.position.y < -4.5f) {
        transform.position = new Vector3(Random.Range(-2f,4f), 5f, transform.position.z);
    }
}

> Assets/Respawn.cs(1,6): error CS0116: A namespace can only contain types and namespace declarations What now?

It needs to be in a C# class. Create a new C# script which will also generate the class code, delete the start and update functions and add this instead.