Hi, I’m new to Unity and coding in general, and I AM in need for help with this teleportation function, which I’ve been struggling for days.
So as the object hits a target, I want it to teleport between 3 tracks in a random order that I created and continue running for 10 session (each session will go through all tracks). But nothing happens and the object runs past the target object. I greatly appreciate any help or lead in any way, and also, please tell me if there’s a way to improve the efficiency of my script!
public class Teleporting : MonoBehaviour
{
public GameObject mouse;
private GameObject SP1; //Track 1 Starting Position
private GameObject SP2; //Track 2 Starting Position
private GameObject SP3; //Track 3 Starting Position
private int[,] track_teleportation_index;
void Start()
{
mouse = GameObject.Find("mouse");
SP1 = GameObject.Find("SP1");
SP2 = GameObject.Find("SP2");
SP3 = GameObject.Find("SP3");
//initialize random teleportation
int[,] track_teleportation_index = new int[10, 3];
System.Random rnd = new System.Random();
var values = Enumerable.Range(1, 3).ToList();
for (int row = 0; row < track_teleportation_index.GetLength(0); row++)
{
values = values.OrderBy(x => rnd.NextDouble()).ToList();
for (int col = 0; col < track_teleportation_index.GetLength(1); col++)
{
track_teleportation_index[row, col] = values[col];
}
}
}
void OnTriggerEnter(Collider other)
{
for (int row = 0; row < track_teleportation_index.GetLength(0); row++)
for (int col = 0; col < track_teleportation_index.GetLength(1); col++)
{
if (track_teleportation_index[row, col] == 1)
{
mouse.transform.position = SP1.transform.position;
}
else if (track_teleportation_index[row, col] == 2)
{
mouse.transform.position = SP2.transform.position;
}
else if (track_teleportation_index[row, col] == 3)
{
mouse.transform.position = SP3.transform.position;
}
}
}
}