Random Teleport Character issue...Help Please.
Hi everyone, I'm starting to take my first steps on unity and I'm trying to make a simple 2d game. I would like to create a teleport within the map and when my character comes into contact with a point he is able to teleport between the various points on the map randomly. As if it were a portal. Now I have managed to insert a list with the points where it can move and with the Random function I can move it. The problem is that the random function also calls the point where my character is already and therefore does not move. This is what I would like to do: If I have three points A B C my character enters point A and can move to points B and C. If he goes to point B he can move to A and C and so on.
Now my character if he enters point A moves randomly to A B C.
How can I go about fixing it? Thank you.
public class Trasport : MonoBehaviour
{
public List<GameObject> Portal = new List<GameObject>();
public GameObject Player;
public void changePortal()
{
int aCaso = Random.Range(0, Portal.Count);
Player.transform.position = new Vector2(Portal[aCaso].transform.position.x, Portal[aCaso].transform.position.y);
}
}
Answer by fct509 · May 17, 2021 at 09:05 AM
I'm not as familiar with the 2D stuff in Unity, but if this were a 3D, I would brake this into a multi-step process with the use of Triggers (which are just Colliders that have the isTrigger field set to 'true), and I would combine this with a teleport manager. So, when the Colliders get enabled, they will register with the manager, and the manager will give them an id.
Then the teleport process can do something like:
Player enters trigger area, raising an On Trigger Enter message, use it to inform the teleport manager
Teleport manager gets teleport request for player
Teleport manager sees the player isn't registered so it registers that that player is teleporting way from the teleport spot that sent the request
Teleport manager randomly selects a teleport spot until it selects one that isn't the starting location
Teleport manager moves player to the selected teleport location
Old spot raises a On Trigger Exit message, use it to send a message to the teleport manager
Teleport manager gets a player left teleport area, but because the player is registered as initiating a teleport form that location, the teleport manager will just ignore it
New spot raises an On Trigger Enter message and informs the teleport manager
Teleport manager sees that a registered player has suddenly appeared in a teleport area that isn't their starting area, so the teleport manager ignores it.
Player leaves the teleport area raising a On Trigger Exit message, use it to inform the teleport manager
The teleport manager sees that a registered player left a teleport area that they did not start in, so it removes that player's registration, allowing the player to initiate this entire process when they enter any teleport area
By implementing these general steps, you should be able to implement this in a multi-player system. Just remember:
Unknown Player enters teleport => Run teleport process
Known Player enters any teleport => Do Nothing
Known Player exits initial teleport => Do Nothing
Known Player exits different teleport => Erase Player, making them Unknown Player
Hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How I randomise a list of signals in Unity and remove it from the list once the clip is finished? 0 Answers
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
Assign role randomly from array for my online game 2 Answers
How do I get random answers? 2 Answers