2D portal recreation, portals keep teleporting player between them without letting the player move away

Hey,

I am recreating portal in 2D for a small project and the teleportaion works, the only problem is the player teleports tn the other portal and then straight back again, this keeps happening until the game breaks and the player falls through the floor.

Is there a way that I can move the player away from the portal with some speed to stop the constant teleporting from happening?

Would I have to know what wall the portal has been placed on to move the player away from it in the opposite direction?

public class StepThroughPortal : MonoBehaviour {

	public GameObject otherPortal;



	void OnTriggerEnter2D(Collider2D other){
		Debug.Log ("Hit the Portal");
		if (other.tag == "Player") {
			other.transform.position = otherPortal.transform.position + otherPortal.transform.forward*2;
		}
	}
}

Any help at all would be greatly apprecitaed

Cheers

Couldn't you simply make the portal colliders smaller? Or spawn the player a little further away from them?

Uhm... but how/why do you intend to add a script that doesn't exist in the first place??? I mean whichever option you choose, you can't add scripts that don't exist...

1 Answer

1

could each portal have two parts?
so entrance A → exit B
then for the player to trigger B → A they would have to move to entrance B.
as it is now you have entrance A and exit A co-located. as are Entrance B and Exit B.

i’m picturing a mirror / door type portal where the player enters one side and exits the other.
or perhaps a set of teleporter pads where the player stands on the entrance pad and arrives on the exit pad.

perhaps some sort of switch where the player has to hit the switch to teleport. so when they arrive, unless they immediately hit the switch they stay at the new location.
or a timer so they cant reuse the teleport for 10 seconds after arrival.

I write the script in this script and after compiling I want to add it to a gameobject

yes it does