OnTrigger Enter Stay and Exit combination
I am trying to make a character teleport from Transform A to Transform B, and then if the player wants, to be able to exit B then walk back into it to be teleported back to A. A two-way teleport. The following code allows player to transport between the two points, but something is not working because it makes them teleport continuously back and forth between the two points. Something in the Stay and Exit logic is not right?:
public float WaitForSeconds = 5f;
public int spawnDelay = 2;
public Transform spawnpoint;
public Transform teleport, teleportExit;
public bool inTeleport = false;
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
// StartCoroutine (RespawnPlayer());
inTeleport = true;
transform.position = new Vector2 (teleportExit.position.x, teleportExit.position.y);
}
if (target.gameObject.tag == "teleportExit" && inTeleport == false) {
// StartCoroutine (RespawnPlayer());
transform.position = new Vector2 (teleport.position.x, teleport.position.y);
}
}
void OnTriggerStay2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = false;
}
}
public IEnumerator RespawnPlayer () {
Debug.Log ("got to coroutine");
yield return new WaitForSeconds (5f);
}
What if you add a button. Say if player hits space then he will teleport ?
That is a good idea and would simplify things, but I am trying to make the effect happen pretty fast (can go through a bunch of portals quickly) so adding a button would slow the process down too much.
Answer by simplicitydown · Dec 23, 2015 at 01:04 AM
Alright this seems to get the job done:
public float WaitForSeconds = 5f;
public int spawnDelay = 2;
public Transform spawnpoint;
public Transform teleport, teleportExit;
public bool inTeleport = false;
void OnTriggerEnter2D(Collider2D target){
if (target.gameObject.tag == "teleport" && inTeleport == false) {
transform.position = new Vector2 (teleportExit.position.x, teleportExit.position.y);
}
if (target.gameObject.tag == "teleportExit" && inTeleport == false) {
transform.position = new Vector2 (teleport.position.x, teleport.position.y);
}
}
void OnTriggerStay2D(Collider2D target){
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
if (target.gameObject.tag == "teleport") {
inTeleport = true;
}
}
void OnTriggerExit2D(Collider2D target){
if (target.gameObject.tag == "teleportExit") {
inTeleport = false;
}
if (target.gameObject.tag == "teleport") {
inTeleport = false;
}
}
The only thing that is still weird is that the character will teleport upon entering the collider of teleport and teleport exit, but will appear in the middle of the sprite that the collider is attached to ins$$anonymous$$d of the collider itself, making the animation look a little odd. Also I don't know if the OnStay and OnExit are working, because the boolean of inTransport never seems to make it to true. That would be nice to figure out, but essentially, the above code still gets the job done.
Answer by Zoelovezle · Dec 24, 2015 at 03:14 AM
Try this and say if you got any problems with using this or using array for adding more locations
// You can set as many teleport locations using this . For that you can just use array.
//Set your your locations here
public Transform spawnPoint1 ;
public Transform spawnPoint2 ;
//Custom delay for telportation.The less the fast
public float addCustomeTeleportationTime ;
private bool canTeleport ;
private float _time ;
void Start()
{
_time = Time.time ;
}
void OnTriggerEnter(Collider2D col)
{
if ((col.tag == "teleport") && Time.time > _time + addCustomeTeleportationTime )
{
Teleport(col);
_time = Time.time ;
}
}
void Teleport(Collider2D colData)
{
if(colData.name == spawnPoint1.name) // you can actually use any condition to check ,.. as per your requirement
{
transform.position = spawnPoint2.position ;
}
else if(colData.name == spawnPoint2.name)
{
transform.position = spawnPoint1.position ;
}
}