Home > Enterprise >  How do i teleport my game object to a specific location in unity
How do i teleport my game object to a specific location in unity

Time:07-22

basically what i wanted to do is to teleport my cube randomly into a specific location

sorry, i have completely no idea how do i teleport my game object into a specific location. ive tried some code, it seems to make sense for me but it still does return an error, this is my code below:

public Transform cubepos; //My cube position
public Transform pos1;//teleport position 1
public Transform pos2;// teleport position 2
public Transform pos3;// and so on
public Transform pos4;
public Transform pos5;
public Transform pos6;
public Transform pos7;
public Transform pos8;
public Transform pos9;
public Transform pos10;

// im pretty sure that i dont need to create a variable to reference each of my 
teleport position for my cube, maybe some of you guys could tell me what should i do so 
i can go into the specific teleport location without having to make a variable for each 
teleport location...?


void Update()
{
    int pos = Random.Range(1, 12);

    for (int i = 0; i < 13; i  )
    {
        switch (pos)
                {
                    case i:
                        cubepos.position = // how do i set my cube position based on
                                           the pos that returned an integer? like if it 
                                             returns 8, i would have to set my cube 
                                            position into pos8.position and i have to 
                                             check every number, that wouldve wasted my
                                              time a lot typing that code;
                }
    }

    
}

CodePudding user response:

You either have to use the variable names for everything, and then you're stuck with a switch case, or you can put those transforms in a List and access the List by index.

With the List you can do it two ways - the harder way would be to keep your code as-is, make a list, and initialize the List in Start, like:

// your transforms
private List<Transform> transforms;

void Start()
{
    transforms = new List<Transform>()
    {
        pos1,
        pos2,
        // ...
        pos10
    };
}

then you can do:

void Update()
{
    int pos = Random.Range(1, 12);
    cubepos.position = transforms[pos-1].position;
}

Be careful here because your Random.Range is 1 to 12 but you only have the 10 transforms, so I'd expect you to get an out-of-range exception when you get returns on 11/12. Might be better to define it as

int pos = Random.Range(0, transforms.Count-1);
cubepos.position = transforms[pos].position;

The other easier way is to just ditch all the other transforms, pos1, etc., and use the list:

public class TransformList : MonoBehaviour
{
    public List<Transform> transforms = new List<Transform>();
    
    void Update()
    {
        int pos = Random.Range(0, transforms.Count-1);
        cubepos.position = transforms[pos].position;
    }
 
    // Other stuff
}

Once you do this you'll see your list in the Inspector window:

Inspector window

Then you can click the > glyph and expand it, then the button to add transforms:

Adding entries

Then you can add as many as you want. By keeping the int pos definition linked to transforms.Count instead of a hard-coded value you can modify that list as you want and everything will continue to run.

Full list

CodePudding user response:

Replace all your transforms pos1-pos10 with the array of transforms.

[SerializeField]
Transform[] _allPossiblePositions;

Add all possible positions to this array in the inspector. Then your random value will be just an index of the element in the array.

int index = Random.Range(0, _allPossitblePositions.Length);
Transform newPos = _allPossibleTransforms[index];

myAwesomeCubeTransform.position = newPos.position;
  • Related