• 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 Blublok · Feb 05, 2016 at 02:34 AM · c#collisioncolliderteleportportals

Making Portals

Heyho dear community

I have a problem with teleporting the character (FPSController) when it collides with the portal. My code looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class Portals : MonoBehaviour {
 
     public GameObject Portal1;
     public GameObject Portal2;
 
     void Awake(){
 
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
  
     }
 
     void OnCollisionEnter (Collision col){
     
         if (col.gameObject == Portal1) {
             GameObject.Find("FPSController").transform.position = Portal2.transform.position;
         }
 
         if (col.gameObject == Portal2)
         {
             GameObject.Find("FPSController").transform.position = Portal1.transform.position;
         }
     }
 
 
 }

I would be glad if you'll help me out. Thanks in advance :)

Comment
Add comment · Show 6
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 Blublok · Feb 01, 2016 at 07:30 PM 0
Share

I totally forgot to mention my problem. When the FPSController collides with the Portal nothing happen.

avatar image DiGiaCom-Tech · May 27, 2016 at 02:55 PM 0
Share

@Blublok ... I see that you are new here. It is customary to respond to followers that help you by voting up/down their answers and/or rewarding them for their efforts.

Please take a look at... Recognise good content and helpful users

... it's near the bottom of this long page ;)

avatar image cjdev · May 28, 2016 at 06:06 AM -1
Share

This question is over 3 months old, try commenting on something newer if you expect a response.

avatar image DiGiaCom-Tech cjdev · May 30, 2016 at 04:13 PM 0
Share

@cjdev ... $$anonymous$$aybe they will be back, maybe they won't, that's not the issue. OTHERS are scanning these questions for answers vs. posting a new question that has already been asked 100 times. What if someone views this question (as I did) and found nothing? I'm sure they would appreciate an answer that is rated, commented, and marked as correct. What if the poster has been returning daily for the past three months hoping that someone had answered their question but found nothing? Do you expect they wild ever come back? Why would anyone waste any more time on a resource that was non-productive?

I ask you ... Are we not encouraged/expected to try and answer and help others with ALL questions we have expertise with? Are we not encouraged/expected to rate & reward the posted answers & comments from others regardless of their age? Isn't doing so in keeping with the supportive nature of this forum?

By simply looking at Blublok's stats you might have figured out that they were new here with little or no experience and might not know how these forums are supposed to work. Being supportive, I'm simply trying to educate anyone who reads this question as to what is expected of us all. Unfortunately, after looking at your stats, I guess my expectation that experienced posters know how these forums are supposed to work is set too high.

avatar image Blublok DiGiaCom-Tech · May 30, 2016 at 04:45 PM 0
Share

Thanks for the information, but I already knew that. I didn't upvote cjdev's because I was waiting for more answers by other users.

avatar image DiGiaCom-Tech Blublok · May 30, 2016 at 06:53 PM 0
Share

@Blublok ... none of that was directed at you ;) If you have any questions related to my code snippet (i.e. multiple or random destinations) don't hesitate to ask!

ThanX for the vote ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DiGiaCom-Tech · May 26, 2016 at 03:44 PM

@Blublok ... I am assuming the Player (P) is coming in contact with a Collider (C) of some kind and is then instantly teleported to Destination (D).

Here is my code (written for 3D but you can easily change it to 2D)...

 using UnityEngine;
 using System.Collections;
 
 public class Teleport : MonoBehaviour {
 
     public Transform Destination;       // Gameobject where they will be teleported to
     public string TagList = "|Player|Boss|Friendly|"; // List of all tags that can teleport
 
     // Use this for initialization
     void Start () {
         // As needed
     }
     
     // Update is called once per frame
     void Update () {
         // As needed
     }
 
     public void OnTriggerEnter(Collider other)
     {
         // If the tag of the colliding object is allowed to teleport
         if (TagList.Contains(string.Format("|{0}|",other.tag))) {
             // Update other objects position and rotation
             other.transform.position = Destination.transform.position;
             other.transform.rotation = Destination.transform.rotation;
         }
     }
 }

Note that I am checking to see who is allowed to use the teleporter. If you want to allow others through then add their tag names to the list (delimited by pipes |). Also note that the destination is a separate object that needs to be placed away from the collider (and above the ground so the player won't fall through it).

This code is placed on the source teleport object not the destination object. No code needs to be placed on the player or other objects that can use the teleports.

Here is the setup...

alt text

Note that the destination of TeleportPad1 is Destination2...

alt text

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 ismaelnascimento01 · May 27, 2017 at 10:20 AM 0
Share

Good alternative !

avatar image
-1

Answer by cjdev · Feb 05, 2016 at 03:52 AM

OnCollisionEnter only works for the collider of the GameObject the script is attached to. Judging by the way you're using GameObject.Find I'm guessing that Portals isn't attached to the FPSController which means that the controller's collision isn't triggering the logic to switch places. For it to work you can attach it to your character controller or you could also create a script for each portal that handles it's own collision.

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 DiGiaCom-Tech · May 26, 2016 at 03:48 PM 1
Share

Adding a script to the character controller requires more work (i.e. I collided with something and now I need to get details from it telling me where to go). As most teleports only go to a single destination it's easier to put the teleport code on the teleporter object (e.g. door, archway, pad, booth, etc.) than on the player. Please see my complete answer below.

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

6 People are following this question.

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

Related Questions

Toggle collider with key 0 Answers

How to get contact points between a mesh collider and a line? 1 Answer

Can't make player character teleport from one side of screen to another. 1 Answer

Set variable to position of collided object. 0 Answers

How do I use the capsule Collider? 1 Answer

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