- Home /
Teleport Not Working in Unity 2D
So I'm basically trying to get my character to "use" a set of stairs by teleporting from one stairway door to another. Each stairway door will be paired with one other stairway door.
Here's the code, I feel like it should be working...my Debug.Logs are even outputting the correct information. Also FYI, I have the Transform doorPairedWith as public so that I can just drag and drop the stairway door that each one is supposed to be paired with.
using UnityEngine;
public class Stairs : MonoBehaviour {
Player player;
public Transform doorPairedWith;
Vector3 doorPairedWithPosition;
Vector3 playerPosition;
BoxCollider2D playerCollider;
BoxCollider2D stairsTriggerCollider;
bool playerIsNearStairs = false;
void Start()
{
player = Player.instance;
playerPosition = player.transform.position;
doorPairedWithPosition = doorPairedWith.position;
playerCollider = player.GetComponent<BoxCollider2D>();
stairsTriggerCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
UseStairs();
}
void UseStairs()
{
if (playerIsNearStairs == true)
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Attempting to use stairway door named: " + doorPairedWith);
playerPosition = doorPairedWithPosition;
}
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerIsNearStairs = true;
Debug.Log("Player is near stairs: " + playerIsNearStairs);
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerIsNearStairs = false;
Debug.Log("Player is near stairs: " + playerIsNearStairs);
}
}
}
Answer by $$anonymous$$ · Jul 03, 2018 at 12:20 AM
I happen to have code that can do just what you want it too! Like, no joke, exactly what you want to do (assumeing I understood you) is the purpose of this code:
The variable playerchar needs to be the gameobject your using as player the variable door is the door you go to in order to teleport the target door is the door you end up at to get this to work, you must press e when inside the trigger area of the door.
if it does not work tell me, as it works perfectly for me. So if it wont work for you then you must have done something wrong elsewhere.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorUniversal : MonoBehaviour {
public GameObject PlayerChar;
public bool InsideTriggerArea = false;
public GameObject Door;
public GameObject TargetDoor;
void OnTriggerEnter2D(Collider2D Door)
{
Debug.Log("Entered");
InsideTriggerArea = true;
}
void OnTriggerStay2D(Collider2D Door)
{
Debug.Log("Stayed");
InsideTriggerArea = true;
if (Input.GetButtonDown("Interact"))
{
Debug.Log("Using Door");
PlayerChar.transform.position = new Vector4(TargetDoor.transform.position.x, TargetDoor.transform.position.y, TargetDoor.transform.position.z);
}
}
void OnTriggerExit2D(Collider2D Door)
{
Debug.Log("Left");
InsideTriggerArea = false;
}
void FixedUpdate()
{
}
}
Awesome, thank you! I ended up getting a different answer elsewhere before I saw this, but what you have is basically what I did:
using UnityEngine;
public class Stairs : $$anonymous$$onoBehaviour {
Player player;
public Transform targetDoor;
bool playerIsNearStairs = false;
void Start()
{
player = Player.instance;
}
void Update()
{
UseStairs();
}
void UseStairs()
{
if (playerIsNearStairs == true)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
Debug.Log("Attempting to use stairway door named: " + targetDoor);
player.transform.position = new Vector3(targetDoor.transform.position.x, targetDoor.transform.position.y - 0.13f);
}
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerIsNearStairs = true;
Debug.Log("Player is near stairs: " + playerIsNearStairs);
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerIsNearStairs = false;
Debug.Log("Player is near stairs: " + playerIsNearStairs);
}
}
}
And just a heads up, the "-0.13f" was because my player was a little bit above the ground every time he teleported to a new location, so I'm just accounting for that.
Just curious @CiaranSimpson , have you figured out a way to tell enemies which set of stairs to use, depending on where they're trying to get to? That's my next goal and it seems like it will be a tough one to figure out.
Just so you know, Unity Answers is supposed to be for questions and answers, this is going into the realm of discussion now (Which is what forums is for, so I would recommend if this door system of yours creates issues further, make a thread about it on the forums, rather then many questions, so you can discuss you issues and intentions in greater depth)
But to answer you question, you could have a public variable with several possible states, with a switch statement, where each state refers to a different door leading to a different location, and you can change the variable as per required. But I am unsure if this would work, having AI issues myself right now so, probably not a great person to ask. (-: