• 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 unity_O9yFOEld5ESRvw · Jun 30, 2018 at 05:53 AM · 2d gamerigidbody2dbeginnervector2teleport

Teleport Player in 2D game?

Hello, I'm an extreme Unity-beginner, and what I want to do is teleport my 2D character to a specific set of coordinates. However, if I access their transform.position and set it equal to the corresponding Vector 2, my character starts to fly upwards, considering the fact that they have a rigidbody attached. Is there any way for me to teleport my player without this happening? Update: I have already tried a Vector 3 instead of a Vector2, and I have also tried editing the Rigidbody's position as opposed to the players. Both of them result in the "floating upwards" effect.

Comment
Add comment · Show 2
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 CardboardComputers · Jul 01, 2018 at 04:52 AM 0
Share

When you set the transform.position, does the character move to the coordinates and then fly up, or does it just fly up? In what manner does it fly up (i.e. does it "jump" up, or does it accelerate up)? I assume your character has a collider and is standing on something with a collider. $$anonymous$$ake sure you don't teleport your character into collider! That'd cause your player to get launched, possibly upwards.

avatar image unity_O9yFOEld5ESRvw CardboardComputers · Jul 01, 2018 at 05:43 AM 0
Share

$$anonymous$$y character moves to the coordinates and then starts to accelerate upwards, however I doubt they are teleporting into a collider as there are no other colliders in the scene, just the player and the ground upon which they're located.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Grocho · Jun 30, 2018 at 08:44 AM

Transform.position is a Vector3.

Change your Vector 2 to a Vector3.

The reason for this is because the 2D engine is just the 3D engine, but you are playing with flat images.

Comment
Add comment · Show 1 · 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 unity_O9yFOEld5ESRvw · Jul 01, 2018 at 01:56 AM 0
Share

Unfortunately, I've already tried this, it has no effect. Thanks anways!

avatar image
0

Answer by HaydeLudos · Jun 30, 2018 at 08:45 AM

Try using rigidbody.position instead

GetComponent().position = newPosition;

If you want to continuously move a rigidbody use MovePosition instead, which takes interpolation into account.


For more details: https://docs.unity3d.com/ScriptReference/Rigidbody-position.html https://docs.unity3d.com/ScriptReference/Rigidbody2D-position.html

Comment
Add comment · Show 1 · 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 unity_O9yFOEld5ESRvw · Jul 01, 2018 at 01:58 AM 0
Share

Unfortunately, I have also tried this; it has no effect.

avatar image
0

Answer by $$anonymous$$ · Jul 02, 2018 at 12:08 PM

Try this.

   Player.transform.position = new Vector4(x,y,x);


make sure somewhere in your code you have Public GameObject Player; and that in inspector you told the game that the variable Player is reffering to your Player.

Player can be whatever name you want but Player is nice and easy for this example.

In the vector4, the x y z can be replaced with your desired x and y co-ordinates, as you are doing 2d the z can be set to 0.

Bassically, what this code does is it access s the position of the player, and replaces its current position with a whole new position that it stored in a new variable, (hence, new vector4)

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 Priyanka-Rajwanshi · Jul 02, 2018 at 05:17 PM

@unity_O9yFOEld5ESRvw After assigning the position, try adding

   rigidbody.Sleep();
   rigidbody.velocity = Vector3.zero;
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

109 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

Related Questions

Player slows down every time they are movies? 0 Answers

How can I tweak acceleration and deceleration of a Rigidbody2D with .AddForce()? 0 Answers

My character won't stop jumping!!! 1 Answer

2D game - Sprite deformation when add frce 2 Answers

2D Rubber hands (Weird question) 1 Answer


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