Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BrandonKirkley · Aug 16, 2019 at 03:27 PM · transformpositionteleportkeypress

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;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Moving gameobject relative to resolution 1 Answer

Teleport object to other objects position 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges