Hello! So I’m still getting used to using scripting. What I am trying to do is Teleport the player by Having a trigger box that once the player enters pressing ‘e’ will set the player’s position the same as the target box. Unfortunately it’s not teleporting my player. I placed the Debug logs to let me know when things are registering. The player does not have a rigidbody. I have tried a lot of different methods but even with both entering the triggerbox and the button press being registered, still no teleport happening. I have a feeling is something real simple but I have spent much longer than I have wanted trying to solve this on my own.
public class Teleport : MonoBehaviour
{
public Transform Player;
public Transform Target;
void Start()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Pressed");
Player.transform.position = Target.transform.position;
}
}
}
Your if (Input.GetKeyDown(KeyCode.E)) is located in the OnTriggerEnter method. You’re essentially asking the player to press the E key at the exact frame that he enters this collider, which just won’t happen.
Instead of doing this, try moving your if statement in the update and creating a bool variable that lets you know if the player entered the collider. Something like this
private bool entered;
private ovid Update()
{
if (entered && Input.GetKeyDown(KeyCode.E))
{
// Teleport?
}
}
// Don't forget to add the OnTriggerExit to set the var back to false
private void OnTriggerEnter(Collider other)
{
entered = true;
}