Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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, 2019 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, 2019 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, 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).

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.

Follow this Question

Answers Answers and Comments

198 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

Related Questions

Player does not teleport to the right location 0 Answers

Teleportation problem! 1 Answer

Portal between scenes? 3 Answers

how would I use teleport in in procedural generated rooms 1 Answer

How can I make a collision sensitive teleporter for a 2D game #c 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges