Random Teleport Character issue

Viewed 139

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. When my character comes into contact with a point he would be able to teleport between the various points on the map randomly as if it were a portal.

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 already is 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?

public class Transport : 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);
    }
}

this is the piece there code where the error appears

public prova ipa = null;

void Teleport()
{
       if ((CATTURA.phase == InputActionPhase.Performed) && (contatto == true))
       {

            if (ipa != null)
            {
                ipa.changePortal(); ***this is the code for teleporting 
    
        *** error CS7036: There is no argument given that corresponds
        to the required formal parameter 'sourcePortal' of
            'prova.changePortal(GameObject)’***
       
        }
       }
}
2 Answers

In your line:

int aCaso = Random.Range(0, Portal.Count);

0 to Portal.Count will always include all portals.

The issue is that the pool of portals you can teleport to (target) still includes the portal you entered (origin).

You may create a new list of portals that removes the source portal as such:

// Pass the origin portal (where your player enters)
public void changePortal(GameObject sourcePortal)
{
    // create a new list where sourcePortal is excluded
    var possibleTargets = Portal.Where(o => o != source).ToList(); 

    // random index from new list
    int aCaso = Random.Range(0, possibleTargets .Count);

    // use the random index to get target portal
    Player.transform.position = new Vector2(possibleTargets [aCaso].transform.position.x, possibleTargets [aCaso].transform.position.y);
}

I do not know how you have this map set up, but you could have that random teleport feature transform your position using an array. Try to create an array for each teleportation pad, and with that do not include the current point. That may prevent teleporting to the same place or try to set a minimum teleportation distance, so that it searches for pads outside of that range.

I would look here: https://answers.unity.com/questions/589808/choose-random-gameobject-from-array.html

In this case they use spawn points, but I am sure you could use it for teleportation with your current code as well.

Related