void OnTriggerEnter2D(Collider2D other) { //Make sure the object is allowed to go through the portal before we let it CanEnterPortal portalObject = other.GetComponent <CanEnterPortal>(); if (portalObject != null) { //Make the portal edges and position them correctly top = Instantiate(portalEdgePrefab.gameObject).GetComponent <Rigidbody2D>(); bottom = Instantiate(portalEdgePrefab.gameObject).GetComponent <Rigidbody2D>(); float dx = GetComponent <BoxCollider2D>().size.x *trans.localScale.x / 2 + top.GetComponent <Collider2D>().bounds.size.x / 2; dx *= -1; float dy = GetComponent <BoxCollider2D>().size.y *trans.localScale.y / 2 + top.GetComponent <Collider2D>().bounds.size.y; float a = body.rotation * Mathf.PI / 180; top.GetComponent <Transform>().position = new Vector3(body.position.x + Mathf.Cos(a) * dx + Mathf.Sin(a) * dy, body.position.y + Mathf.Cos(a) * dy + Mathf.Sin(a) * dx, 0); bottom.GetComponent <Transform>().position = new Vector3(body.position.x + Mathf.Cos(a) * dx + Mathf.Sin(a) * -dy, body.position.y + Mathf.Cos(a) * -dy + Mathf.Sin(a) * dx, 0); top.rotation = body.rotation; bottom.rotation = body.rotation; //Turn off collision to allow it through Physics2D.IgnoreCollision(other.GetComponent <Collider2D>(), on, true); //Duplicate the object going through the portal, so that it will appear at both ends portalObject.updateOther(this, otherPortal); } }
void OnTriggerEnter2D(Collider2D other) { //Make sure the object is allowed to go through the portal before we let it CanEnterPortal portalObject = other.GetComponent <CanEnterPortal>(); if (portalObject != null && otherPortal != null) { //Teleport to other portal //Position it correctly Rigidbody2D objectBody = portalObject.GetComponent <Rigidbody2D>(); float relX = objectBody.position.x - body.position.x; //positive if to the right float relY = objectBody.position.y - body.position.y; //positive if above float a = body.rotation * Mathf.PI / 180; float dx = Mathf.Cos(a) * relX + Mathf.Sin(a) * relY; // + body.GetComponent<BoxCollider2D>().size.x * body.transform.localScale.x; float dy = Mathf.Cos(a) * relY + Mathf.Sin(a) * relX; // dy *= -1; float a2 = otherPortal.body.rotation * Mathf.PI / 180; float x = otherPortal.body.position.x + Mathf.Cos(a2) * dx + Mathf.Sin(a2) * dy; float y = otherPortal.body.position.y + Mathf.Cos(a2) * dy + Mathf.Sin(a2) * dx; objectBody.position = new Vector3(x, y, 0); //TODO: Transfer velocity //TODO: Angels //Note that velocities are equal of portal is pointing opposite direction } }
public void updateOther(Portal enter, Portal exit) { if (other == null) { Rigidbody2D bodyEnter = enter.GetComponent <Rigidbody2D>(); Rigidbody2D bodyExit = exit.GetComponent <Rigidbody2D>(); Rigidbody2D body = GetComponent <Rigidbody2D>(); //Duplicate self other = Instantiate(gameObject).GetComponent <CanEnterPortal>(); //Ignore collisions with the other wall Physics2D.IgnoreCollision(other.GetComponent <Collider2D>(), exit.on); Rigidbody2D bodyOther = other.GetComponent <Rigidbody2D>(); other.other = this; //Position it correctly //float dx = bodyExit.GetComponent<Collider2D>().bounds.size.x; //TODO: Use angle instead if (exit.isLeft) { // dx *= -1; } float relX = body.position.x - bodyEnter.position.x; //positive if to the right float relY = body.position.y - bodyEnter.position.y; //positive if above float a = bodyEnter.rotation * Mathf.PI / 180; float dx = Mathf.Cos(a) * relX + Mathf.Sin(a) * relY + bodyEnter.GetComponent <BoxCollider2D>().size.x *bodyEnter.transform.localScale.x; float dy = Mathf.Cos(a) * relY + Mathf.Sin(a) * relX; float a2 = bodyEnter.rotation * Mathf.PI / 180; float x = bodyExit.position.x + Mathf.Cos(a) * dx + Mathf.Sin(a) * dy; float y = bodyExit.position.y + Mathf.Cos(a) * dy + Mathf.Sin(a) * dx; bodyOther.position = new Vector3(x, y, 0); //Transfer velocity //TODO: Angels //Note that velocities are equal of portal is pointing opposite direction bodyOther.velocity = body.velocity; } }
void OnTriggerExit2D(Collider2D other) { CanEnterPortal portalObject = other.GetComponent <CanEnterPortal>(); if (portalObject != null) { Destroy(top.gameObject); Destroy(bottom.gameObject); Physics2D.IgnoreCollision(other.GetComponent <Collider2D>(), on, false); //TODO: Make this work with angles /* * Pseudocode * * Get angle between center of portal and center of object * If this angle between angle of portal +-90, we are good to go * * */ Rigidbody2D otherBody = other.GetComponent <Rigidbody2D>(); float dx = otherBody.position.x - body.position.x; float dy = otherBody.position.y - body.position.y; float a = body.rotation; // in degrees float a0 = a - 90; float a2 = a + 90; float a1 = Mathf.Atan(dy / dx) * 180 / Mathf.PI; //from -90 to + 90 if (dx < 0) { a1 += 180; } if (a1 < 0) { a1 += 360; } if (a < 0) { a += 360; } if (a1 - a > -90 && a1 - a < 90) { portalObject.destroyOther(); } else { portalObject.detatch(); Destroy(portalObject.gameObject); } } }