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 /
  • Help Room /
avatar image
0
Question by simplicitydown · Dec 22, 2015 at 11:59 PM · ontriggerenterontriggerexitontriggerstay

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

Comment
Add comment · Show 3
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 Zoelovezle · Dec 23, 2015 at 12:48 AM 0
Share

What if you add a button. Say if player hits space then he will teleport ?

avatar image simplicitydown Zoelovezle · Dec 23, 2015 at 01:55 PM 0
Share

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.

avatar image Zoelovezle simplicitydown · Dec 24, 2015 at 02:46 AM 0
Share

read my answer it may help.. Not sure

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
Comment
Add comment · Show 1 · 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
avatar image simplicitydown · Dec 23, 2015 at 01:07 AM 0
Share

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.

avatar image
0

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

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

33 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

Related Questions

I'm trying to make a climbable ladder, but when I climb down, the character controller falls through the ground, Can anybody help me with this? 0 Answers

Text disappear on OnTriggerExit() 1 Answer

Character controller trigger enter and exit loop 1 Answer

OnTriggerEnter2D not reseting when collider gets disabled 0 Answers

How can I make a collider that gives me stat up to stop giving me it? 0 Answers


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