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;
}
void TeleportLeft(Vector3 scrPos){
Vector3 goalScrPos = new Vector3 (Screen.width + 10, scrPos.y, scrPos.z);
Vector3 targetWorldPos = Camera.main.ScreenToWorldPoint (goalScrPos);
transform.position = targetWorldPos;
}
when the player gets out of the right side of the screen it comes back from the left and from left to right. this code works only teleportRight but teleportLeft does not, anyone know
s how to fix this?