So basically I have two portals and I cant make them work I’ve been trying for days, but they just don’t seem to work. here’s what my game looks like.
make sure each of the portal objects have a Collider and said collider is set as a trigger
also you need to attach the script to the portal prefabs, and make sure each is set to the global variables:
public class FixPosition : MonoBehaviour {
public GameObject exitPortal;
public GameObject enterPortal;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag==Consts.TAG_LIVE_PLAYER||other.tag==Consts.TAG_LIVE_ENEMY)
{
fixPlayerPosition(other);
}
}
void fixPlayerPosition(Collider2D other)
{
Vector3 exitPosition;
if (gameObject.tag="enter_tag")//make sure you exit in the right portal
{
exitPosition=exitPortal.transform.position;
}
else
{
exitPosition=enterPortal.transform.position;
}
Vector3 position=other.gameObject.transform.position;
Vector3 newPosition=new Vector3();
newPosition.Set(exitPosition.transform.position.x,exitPosition.transform.position.y,
exitPosition.transform.position.z);
other.gameObject.transform.position=newPosition;
}
}
enjoy.
Since you haven’t written any code, I can describe to you a quick pseudo code edition of mine that I would do.
For one, there would be a PortalController.cs script.
The essentials of this PortalController.cs script would look something like this.
class PortalController : MonoBehavior
{
private PortalController connectedPortal;
void Start()
{
// When you start, you want to setup the portal connections, find the desired portal, either through
// tag names or some other way, and set that portals PortalController component to the connectedPortal
// variable
}
OnTriggerEnter2D(Collider2D otherCollider)
{
// Move the player to the connectedPortals transform.position
}
}
So essentially, each portal has a reference to the portal it is connected to, and when something collides with the portal, you use the connectedPortal reference to move the collided object to the other portals transform.position.