- Home /
"teleport" player depend on screen width
I want my sides to be "connected" if the player exits from the right side of the screen he will get out of the left, and the opposite, same as in Doodle Jump
Answer by siaran · Mar 15, 2015 at 06:02 PM
Tranform your player's world position to screen position (Camera.main.WorldToScreenPoint), then check his X-coordinate in screen space. If it's less than 0 he's to the left of your screen, if it's more then Screen.width hes at off screen at the right, so you need to teleport him.
Determining where to teleport him to is essentially the same but in reverse: determine the point on the screen you want to have him appear, then transform that to world coordinates (Camera.main.ScreenToWorldPoint), then teleport your player to that position.
As someone new to Unity I don't understand this too much, could you show me a sample code please?
Sure. In a script atttached to your player:
void Update(){
//get the screen position
Vector3 scrPos = Camera.main.WorldToScreenPoint(transform.position);
//Check if we are too far left
if(scrPos.x < 0) TeleportRight(scrPos);
//check if we are too far right
if(scrPos.x > Screen.width) TeleportLeft(scrPos);
}
//TeleportRight moves character from it's current x position to x position 10 pixels left from the right edge of the screen
void TeleportRight(Vector3 scrPos){
//this is the position on the screen we want to move the character too, we only want to change it's x-coordinate
Vector3 goalScrPos = new Vector3(Screen.width-10, scrPos.y, scrPos.z);
//translate goal screen position to world position
Vector3 targetWorldPos = Camera.main.ScreenToWorldPoint(goalScrPos);
//move player
transform.position = targetWorldPos;
}
Does that help?
Answer by zRevenger · Mar 15, 2015 at 11:15 PM
try to do it in 2 javascripts.the first script:
var leftSide : Transform;
var rightSideScript : NameOfTheScript;
var character : GameObject;
function OnTriggerEnter()
{
if(character.transform.position == leftSide.position)
{
character.transform.position = rightSideScript.rightSide.position;
Debug.Log("Side Changed");
}
}
the second script:
var rightSide : Transform;
var leftSideScript : NameOfTheScript;
function OnTriggerEnter()
{
if(leftSideScript.character.transform.position == rightSide.position)
{
leftSideScript.character.transform.position = leftSideScript.leftSide.position;
Debug.Log("Side Changed")
}
}
now you need to insert the right side script in a gameobject on the right side of the script and the same thing for the left side. to the gameobjects add a box collider and set it as a trigger. on the left side script add the character gameobject and the right side gameobject and add himself. to the right add the left side gameobject and himself. now it showld work.
Answer by AlexV03 · Mar 01, 2021 at 02:02 PM
I am using this script and how can i change it so if the player goes to the right he teleport to the left. My idea was copy and past the TeleportRight() and rename it to TeleportLeft() and make the transform.position = targetWorldPos; to transform.position = - targetWorldPos that works but the players y axis isn't working so if my player has a y value of y-3 he teleports to y3 how to fix that?
Your answer
Follow this Question
Related Questions
Touch Hd screen drag problems 0 Answers
How to scale my camera to fit all android and iOS screen sizes 1 Answer
How to play Unity on Android's background Home Screen? 1 Answer
[Android] How to dim screen without locking device? 3 Answers
What is the update for iPhoneSettings.screenCanDarken 2 Answers