• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WillKlep · Jan 21 at 09:54 PM · 2dteleportteleportingportal

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

} alt text

portalsystem.png (218.2 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sean244 · Jan 22 at 02:46 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jan 22 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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

203 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Teleportation problem! 1 Answer

Portal between scenes? 3 Answers

Teleport 2D Player to another room 2 Answers

Change object coordinates to another ones upon button press 2 Answers

Teleportation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges