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 ![]()
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][1]...not that your solution is necessarily bad. [1]: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
– robertbu