- Home /
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.
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.
$$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.
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.
Unfortunately, I've already tried this, it has no effect. Thanks anways!
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
Unfortunately, I have also tried this; it has no effect.
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)
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;
Your answer
![](https://koobas.hobune.stream/wayback/20220612165749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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
Movement on prefab objects 0 Answers
2D game - Sprite deformation when add frce 2 Answers
How can I increase or decrease jump speed while keeping the jump arc the same? 1 Answer