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; } }