- Home /
Collision for Amount of Time
Hi guys,
I'm making a teleportation system that I need help with.
If the player collides with the portal for x amount of seconds, then the player gets teleported. If the player doesn't collide with the portal for X amount of seconds, or just zooms by the portal, then he doesn't get teleported.
Heres my teleportation script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Teleportation : MonoBehaviour { //portals public GameObject Portal; public GameObject Portal1; public GameObject Portal2;
//players
public GameObject Player1;
public GameObject Player2;
public GameObject Player3;
public GameObject Player4;
public void OnTriggerStay2D(Collider2D other) //checks if player is interacting with the portal
{
if(other.gameObject.name == "Player 1 (Red)")
{
StartCoroutine(TeleportP1()); //uses/calls teleportP1 IEnumerator
}
if(other.gameObject.name == "Player 2 (Blue)")
{
StartCoroutine(TeleportP2());
}
if(other.gameObject.name == "Player 3 (Green)")
{
StartCoroutine(TeleportP3());
}
if(other.gameObject.name == "Player 4 (Yellow)")
{
StartCoroutine(TeleportP4());
}
}
IEnumerator TeleportP1()
{
yield return new WaitForSeconds(1);
//teleportation for all players for Portal1
Player1.transform.position = new Vector2(Portal.transform.position.x, Portal1.transform.position.y);
Player1.transform.position = new Vector2(Portal1.transform.position.x, Portal.transform.position.y);
Player1.transform.position = new Vector2(Portal2.transform.position.x, Portal1.transform.position.y);
}
IEnumerator TeleportP2()
{
yield return new WaitForSeconds(1);
Player2.transform.position = new Vector2(Portal.transform.position.x, Portal1.transform.position.y);
Player2.transform.position = new Vector2(Portal1.transform.position.x, Portal.transform.position.y);
Player2.transform.position = new Vector2(Portal2.transform.position.x, Portal1.transform.position.y);
}
IEnumerator TeleportP3()
{
yield return new WaitForSeconds(1);
Player3.transform.position = new Vector2(Portal.transform.position.x, Portal1.transform.position.y);
Player3.transform.position = new Vector2(Portal1.transform.position.x, Portal.transform.position.y);
Player3.transform.position = new Vector2(Portal2.transform.position.x, Portal1.transform.position.y);
}
IEnumerator TeleportP4()
{
yield return new WaitForSeconds(1);
Player4.transform.position = new Vector2(Portal.transform.position.x, Portal1.transform.position.y);
Player4.transform.position = new Vector2(Portal1.transform.position.x, Portal.transform.position.y);
Player4.transform.position = new Vector2(Portal2.transform.position.x, Portal1.transform.position.y);
}
}
Inside your 'TeleportP1' function, it says "teleportation for all players for Portal1", but you just keep setting the position for Player1, three separate times. You did the same for the other coroutines as well.
Answer by Eno-Khaon · Jan 22, 2019 at 08:24 PM
If I may suggest a rewrite instead, you can grab each player anonymously for a teleport instead by using a script attached to each teleporter:
public class Teleporter : MonoBehaviour
{
// Time until teleport while inside trigger area
public float activationTime = 1.0f;
// Current targets/players without teleporter
List<TeleportationTarget> targets;
// using a Transform allows for a moving,
// 1-way exit point, if desired
Transform destination;
void Start()
{
targets = new List<TeleportationTarget>();
}
void OnTriggerEnter2D(Collider2D other)
{
// Determine whether the target is valid
// A potential example for players only:
if(other.CompareTag("Player"))
{
targets.Add(new TeleportationTarget(other.transform));
}
}
void OnTriggerExit2D(Collider2D other)
{
// Check through the list to find where
// this player was being tracked.
for(int i = 0; i < targets.Count; i++)
{
if(targets[i].target == other.transform)
{
targets.RemoveAt(i);
break;
}
}
}
void Update()
{
// Processed in reverse order because the
// last in would be the last out as well.
// If a target is removed, the iterator will
// still be in the right spot.
for(int i = targets.Count - 1; i >= 0; i--)
{
targets[i].elapsedTime += Time.deltaTime;
if(targets[i].elapsedTime >= activationTime)
{
// Teleportation will result in
// OnTriggerExit2D triggering
// for the player, but a fallback
// removal from the list can
// be added here if necessary
Teleport(targets[i].target);
}
}
}
void Teleport(Transform target)
{
target.position = destination.position;
}
}
public class TeleportationTarget
{
// The target, player or otherwise, for a teleport
public Transform target;
// The time the target has spent near this teleporter
public float elapsedTime;
public TeleportationTarget(Transform target)
{
this.target = target;
elapsedTime = 0.0f;
}
}
It may seem a bit lengthy this way, but it improves efficiency by not needing to call OnTriggerStay2D every frame for every object in range, and it allows for the potential to have non-player objects be teleportable (by changing the if(other.CompareTag("Player"))
line).