Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Cowsmanaut · Mar 03, 2014 at 07:13 AM · c#teleportteleporting

2d game Teleporter works only as a single instance

so, I've created the code and it works ok for the single teleport and exit. but if I duplicate it and on the new version set the new exit.. it still sends the player to the previous exit, which I think I understand why, but would really like a heads up on how to do this better. I'm completely new to C# and only had a basic knowledge of MEL and some simple windows scripting before this so I'm not much of a coder. So I'd really rather examples in C#, because JS or BOO will just confuse me more :P

Second part to this, is WHY am I doing it this way? well after trying various other examples on the 100's of websites I've now visited all weekend.. none of the others worked because of one simple detail.. the onTriggerEnter feature wouldn't work with the 2D Rigid bodies for some odd reason.. the circle or box collider seemed to give me nothing back when I tried to debug.log it.. nada.. just a blank stare.. here's the code.

 using UnityEngine;
 using System.Collections;
 
 public class teleporter : MonoBehaviour
 {
     //this is the exit object
     public Transform exit;
 
     //This is the player
     public Transform TeleportMe;
 
     //Checking player in telelporter
     bool inTeleporter = false;
     float TeleportRadius = 0.2f;
 
     //excluding all but telelporters in a mask
     public LayerMask whatIsTeleporter;
 
     //calling the animator to check the value later
     Animator anim;
 
     void Start () 
     {
         anim = GetComponent<Animator> ();
     }
 
     void FixedUpdate ()
     {
         //this turns on or off IN TELEPORTER value uses "onWallCheck" game object
         inTeleporter = Physics2D.OverlapCircle (TeleportMe.position, TeleportRadius, whatIsTeleporter);
         anim.SetBool ("Teleporter", inTeleporter);
     }
 
     //teleport if in the teleporter this way, because 2D rigid bodies weren't returning trigger
     void Update () 
     {
         if (inTeleporter) 
         {
             anim.SetBool ("Teleporter", false);
             TeleportMe.position = exit.position;        
         }
     }
 }

Thanks in advance :)

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 robertbu · Mar 03, 2014 at 07:21 AM 0
Share

OnTriggerEnter() does not work with 2D, but OnTriggerEnter2D() does...not that your solution is necessarily bad.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cowsmanaut · Mar 04, 2014 at 04:04 AM

excellent, I did find that today and when I tried it initially it didn't seem to work. however it works now and it also solves my other issue at the same time, while at the same time letting make the script far shorter.. thanks :)

 using UnityEngine;
 using System.Collections;
 
 public class teleporter : MonoBehaviour
 {
     //this is the exit object
     public Transform exit;
 
     //This is the player
     public Transform TeleportMe;
 
 
     //teleport if in the teleporter this way, because 2D rigid bodies weren't returning trigger
     void OnTriggerEnter2D(Collider2D other)
     {
             TeleportMe.position = exit.position;        
     }
 }
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 samccloy4 · Jun 01, 2021 at 12:40 PM 0
Share

I keep getting a weird bug where I’m teleported in scene view and can control character but In game mode Im in the background somewhere

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

21 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

Related Questions

Teleporting a character to mouse position 1 Answer

Teleporting Objects,Teleporting Object 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

First person Teleportation 2 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