- Home /
Player teleportation isn't working
I'm quite new to unity and am trying to get the player to teleport when they're within range of a door and when they press the space bar. At the moment, the player teleports regardless of whether they're in the zone or not whenever I press space. I'm not entirely sure what to do. The text seems to work, where it's on screen for as long as the player is within the zone.
This is the code I've written:
public class TeleportDoor: MonoBehaviour { [SerializeField] private Transform Player; [SerializeField] private Transform Telepoint; public GameObject doorText; bool inZone = false;
void Start()
{
doorText.SetActive(false);
inZone = false;
}
void OnTriggerEnter(Collider other)
{
doorText.SetActive(true);
inZone = true;
}
void OnTriggerExit(Collider other)
{
doorText.SetActive(false);
inZone = false;
}
void Update ()
{
if ((inZone = true) && (Input.GetKeyDown("space")))
{
Player.position = Telepoint.position;
}
}
}
Many thanks!
Answer by tormentoarmagedoom · Mar 03, 2020 at 02:23 PM
Hello.
I see so many "errors" .You should take your time and read/watch tutorials and manuals.
OnTriggerEnter(Collider other)
is now triggering with all colliders, you need tyo specify which colliders wil lactivate the function
WE are developing, not playing. You dont teleport anything, you are changing its transform position.
when checking a bool variable value, is better to do this:
if (inZone)
or
if (!inZone)
" ! " means No
Invesitgate and watch more tutorials about Oncollission and Ontrigger functions. Dont try to go fast, you are learniong, take your time, read, try ,watch, try again, try try try.
Good luck!