Asteriods Style Screenwrap (Javascript)

I’ve attached the code below to a ship to get an Asteriods style screenwrap. The bottom, top, and right sides of the screen wrap my ships to the other side of the screen like they should. When I go through the left side though, it doesn’t wrap? Just keeps going. Any help would be really appreciated sense i’m just learning javascript and I don’t fully understand how the script works. Thanks!

Btw, I uploaded the “game” to Kong if you want to play it and see what’s wrong?

var wrap:Vector2;
var wrapMax:Vector2;
function Awake(){
	var wrapmin:Vector3=Camera.main.ScreenToWorldPoint(Vector3(0,0,Camera.main.transform.position.y));
	wrap=Vector3(wrapmin.x-4,wrapmin.z-4);
	var wrapmax:Vector3=Camera.main.ScreenToWorldPoint(Vector3(Screen.width,Screen.height,Camera.main.transform.position.y));
	wrapMax=Vector3(wrapmax.x+4,wrapmax.z+4);
}
		
function Update () {
	if(transform.position.x>wrapMax.x){
		transform.position.x=wrap.x;
	}
	if(transform.position.x>wrapMax.x){
	transform.position.x=wrap.x;
		
	}	
	if(transform.position.z>wrapMax.y){
		transform.position.z=wrap.y;
	}
	if(transform.position.z<wrap.y){
		transform.position.z=wrapMax.y;
	}
}

First, I’d make your variable names more consistent. Specifically, I’d use ‘wrapMin’ rather than ‘wrap’ to represent the minimum bounds of the ‘wrapped’ region.

As for your problem, look very carefully at the four conditionals in your Update() function (you could be using some if-else’s there, but that’s a different issue.)

Here’s a further hint: look carefully at the first two of the four conditionals.

This is what you want: #pragma strict
var wrap:Vector2;
var wrapMax:Vector2;
function Awake(){
var wrapmin:Vector3=Camera.main.ScreenToWorldPoint(Vector3(0,0,Camera.main.transform.position.y));
wrap=Vector3(wrapmin.x-0.2,wrapmin.z-0.2);
var wrapmax:Vector3=Camera.main.ScreenToWorldPoint(Vector3(Screen.width,Screen.height,Camera.main.transform.position.y));
wrapMax=Vector3(wrapmax.x+0.2,wrapmax.z+0.2);
}

function Update () {
if(transform.position.x>wrapMax.x){
transform.position.x=wrap.x;
}
if(transform.position.x<wrap.x){
transform.position.x=wrapMax.x;

}  
if(transform.position.z>wrapMax.y){
   transform.position.z=wrap.y;
}
if(transform.position.z<wrap.y){
   transform.position.z=wrapMax.y;
}

}

Hey! I understand the concept, using my camera as my boundaries I adjust the position using a simple if then command. But I don’t really understand how the code works, and i’ve been trying to look it up. Sooo i’m pretty lost! But I’ll keep trying. Feel free to let me know exactly how. :stuck_out_tongue:

bumpalump!! :frowning: