- Home /
I'm trying to teleport with a key press, but right now they only teleport for one second. How to fix?,With a key press, no OnTriggerEnter, how to teleport the player?
Hi. I am trying to teleport my player to a location by pressing the Q key. I want the player to be able to do this at any time or place, so I'm not using OnTriggerEnter for teleportation.
However, when I click the Q key, the player is at the location for only a millisecond before snapping back to where they were. How do I fix this? I am a beginner, so I'm sorry if its a very obvious solution, ha ha. Thank you so much for your help! Code below. It is attached to my FPSController player object, and teleportTarget is an empty gameobject I've placed at where I want him to be.
public class ChangeLocation : MonoBehaviour
{
public Transform teleportTarget;
private void Update()
{
if (Input.GetKeyDown("q"))
{
Teleport();
}
}
void Teleport()
{
transform.position = teleportTarget.position;
}
}
,Hi, I'm trying to make it so that when the player presses Q, they teleport to a certain location. Problem is, I press Q but the player is only there for a millisecond. GetKeyDown and GetKeyUp both lead to this and GetKeyDown makes the player stay there only for as long as Q is held down.
Trying to make it so the player could teleport anytime or anywhere they want, so I am avoiding the usual OnTriggerEnter to teleport. I'm trying to make it a key press.
I am a beginner so my apologies if I am missing an obvious solution. Thank you for your help! I appreciate it!
public class ChangeLocation : MonoBehaviour
{
public Transform teleportTarget;
private void Update()
{
if (Input.GetKeyDown("q"))
{
transform.position = teleportTarget.position;
}
}
}
Answer by I_Am_Err00r · Aug 16, 2019 at 05:08 PM
That should work, if I had to guess you have the player position being controlled elsewhere on top of this; to help you pinpoint what is going on, where to do you teleport back to after the one second? You're going to have to find out what script is causing the player to restrict player positioning.
Answer by xephosmmb122 · Aug 16, 2019 at 09:11 PM
Hi I did something similar recently,
if (Input.GetKeyDown(KeyCode.LeftControl) && direction == "right" && teleport > 0)
{
Vector3 dashForce = new Vector3(dashSpeed + speed - Time.smoothDeltaTime, 0, 0);
rig.MovePosition(transform.position + dashForce);
teleport--;
}
This would teleport the player in the right direction, for your game I would do
if (input.GetKeyDown(KeyCode.q) {
Rigidbody.Moveposition(teleportTarget.position);
}
I hope this helps!
Your answer
Follow this Question
Related Questions
Making The Hole In A Golf Game 1 Answer
Teleporting Player to a point using box colliders 1 Answer
Transform.Position Help 3 Answers