- Home /
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 :)
OnTriggerEnter() does not work with 2D, but OnTriggerEnter2D() does...not that your solution is necessarily bad.
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;
}
}
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
Follow this Question
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