- Home /
Question by
SirBoboHobo · Mar 17, 2015 at 03:22 PM ·
screenteleportscreentoworldpoint
WorldToScreenPoint teleport left
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?
Comment
Best Answer
Answer by laradov · Mar 17, 2015 at 04:23 PM
In TeleportLeft you teleport the object to the right of the screen by Screen.width + 10. Try just 10:
void TeleportLeft(Vector3 scrPos){
Vector3 goalScrPos = new Vector3 (10, scrPos.y, scrPos.z);
Vector3 targetWorldPos = Camera.main.ScreenToWorldPoint (goalScrPos);
transform.position = targetWorldPos;
}
Your answer
Follow this Question
Related Questions
iPad touch is offset! Unity2d. 2 Answers
Viewport to World Coordinates 2 Answers
How to set screen boundaries? 1 Answer
"teleport" player depend on screen width 3 Answers
Accurate On-Screen Bounding Boxes 0 Answers