Can't make player character teleport from one side of screen to another.
Please guys if this is a new question i'm sorry this is literally my first time making a game.
TL;DR: Need to make player get out of one side of the camera view and appear in the other side, I'm using OnTriggerEnter2D
but can't get the Player game object Transform, to alter it's value.
So I'm making a doodle jump style game, I've already came pretty far, I have animations, one way collision platforms. the bounce is just right, and i can spawn a bunch of platforms and make the screen follow the player.
I'm now trying to make the player enter in one side of the screen (camera view) and come out of the other keeping it's height and velocity. I've looked into a lot of tutorials, into forums Youtube tutorials, but i just cannot get how i can do it.
This is currently my code
public class TeleportFunction : MonoBehaviour
{
public Transform LeftWall;
public Transform RightWall;
void OnTriggerEnter2D(Collider2D Player)
{
if (Player.gameObject.tag == "Left")
{
Debug.Log("Left");
}
else if (Player.gameObject.tag == "Right")
{
Debug.Log("Right");
//Transform = RightWall;
}
}
}
In this Debug.Log every time my player hits the box collider I've set in the edges of screen it shows the correct side in console. But when i try to use Get.Componet <Transform>
to get the player position and change it to the values of either public Transform LeftWall;
or public Transform RightWall;
It doesn't work. I've tried to just get the this values trough the "Player" on the OnTriggerEnter2D
but with no success either.
If anyone knows this can you help Me ?
Answer by mattanmohel · Jan 25, 2021 at 02:21 AM
In this case, I would use the mod to clamp the player between the two sides of the screen and teleporting it in between. Rather than use colliders, I would check where the player is relative to the camera. From there you could say that the player has to be greater than (Camerapos.x - Orthographic size) and less than (Camerapos.x + orthographic size), where the ortho size is half the camera's view, accessible through Camera.main.orthographicsize. Now to calculate and teleport the character you need to do, assuming "size" is 2 camera.orthographicsize: Player Pos - (size Mathf.FloorToInt(PlayerPos/(size + "left point"))). What this does is say that as long as the player is less than the camera max and greater than the camera min, Mathf.FloorToInt(PlayerPos/(size + "left point")) returns zero. therefore the playerpos remains the same, however if the playerpos is out of the bounds of the camera, the Floor returns either 1 or -1, then subtracts the size of the camera from the player pos, which should teleport it to the other side if out of bounds.
Your answer
Follow this Question
Related Questions
Collision With Text 0 Answers
How to set box collider to a gizmos cube size 0 Answers
how to teleport an object,want to teleport an object but it won't work 1 Answer
Teleport script help! 1 Answer
need help teleporting an object 0 Answers