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 :)
I totally forgot to mention my problem. When the FPSController collides with the Portal nothing happen.
@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 ;)
This question is over 3 months old, try commenting on something newer if you expect a response.
@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.
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.
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...
Note that the destination of TeleportPad1 is Destination2...
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.
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.