Hi, I’m really new to C# and Unity, so forgive me if I made a stupid mistake. I’m trying to teleport my player from 1 place to another. For this I made two objects and I check collision (and if the space bar is pressed) with them. If there is collision (and space is pressed), then the player teleports to the other objects location. However, in my case, if I teleport more than two times to any other object, 1 object keeps teleporting me to itself, and the other one is still working properly. Which objects works and which not depends on the first one I enter in the game. I use the same code for both teleporters. (‘GameObject portal’ is the destination of the player, which is the other object.)
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
doorCollision = true;
}
}
void Update()
{
if ((Input.GetKey("space")) && (doorCollision))
{
StartCoroutine("Teleport");
}
}
IEnumerator Teleport()
{
yield return new WaitForSeconds(tpTime);
player.transform.position = new Vector2(portal.transform.position.x, portal.transform.position.y);
}
}