EDIT :
To use Camera.main.ScreenToWorldPoint
, pass a Vector3 and specify a screen-pixel for X and Y, for Z you need to specify the distance between the camera and the spaceship. e.g. if the camera Z is -10 , and the ship Z is 0, then the distance is 10. so the command should read : leftConstraint = Camera.main.ScreenToWorldPoint(new Vector3(0.0f, 0.0f, 10.0f)).x;
Here’s a working C# script :
using UnityEngine;
using System.Collections;
public class Screen_Bounds : MonoBehaviour
{
public float leftConstraint = 0.0f;
public float rightConstraint = 0.0f;
public float buffer = 1.0f; // set this so the spaceship disappears offscreen before re-appearing on other side
public float distanceZ = 10.0f;
void Awake()
{
// set Vector3 to ( camera left/right limits, spaceship Y, spaceship Z )
// this will find a world-space point that is relative to the screen
// using the camera's position from the origin (world-space Vector3(0,0,0)
//leftConstraint = Camera.main.ScreenToWorldPoint( new Vector3(0.0f, 0.0f, 0 - Camera.main.transform.position.z) ).x;
//rightConstraint = Camera.main.ScreenToWorldPoint( new Vector3(Screen.width, 0.0f, 0 - Camera.main.transform.position.z) ).x;
// using a specific distance
leftConstraint = Camera.main.ScreenToWorldPoint( new Vector3(0.0f, 0.0f, distanceZ) ).x;
rightConstraint = Camera.main.ScreenToWorldPoint( new Vector3(Screen.width, 0.0f, distanceZ) ).x;
}
void Update()
{
/*
if (shipX < leftConstraint - buffer) { // ship is past world-space view / off screen
shipX = rightConstraint + buffer; // move ship to opposite side
}
if (shipX > rightConstraint + buffer) {
shipX = leftConstraint - buffer;
}
*/
}
}
Original Answer :
You can find a position in world-space that the spaceship moves off screen, then move the spaceship to a position on the other side that is off-screen. Use Camera.main.ScreenToWorldPoint
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
Here is a JS example (just saw it was C#, have added below). Also consider the Update functions as psuedoCode =]
var leftConstraint : float = 0.0;
var rightConstraint : float = 960.0;
var buffer : float = 1.0; // set this so the spaceship disappears offscreen before re-appearing on other side
function Start()
{
// set Vector3 to ( camera left/right limits, spaceship Y, spaceship Z )
// this will find a world-space point that is relative to the screen
leftConstraint = Camera.main.ScreenToWorldPoint(Vector3(Screen.width * 0.0, 0.0, 0.0)).x;
rightConstraint = Camera.main.ScreenToWorldPoint(Vector3(Screen.width * 1.0, 0.0, 0.0)).x;
}
function Update()
{
if (shipX < leftConstraint - buffer) { // ship is past world-space view / off screen
shipX = rightConstraint + buffer; // move ship to opposite side
}
if (shipX > rightConstraint + buffer) {
shipX = leftConstraint - buffer;
}
}
Converted : C# is not my native language, but try :
float leftConstraint = 0.0f;
float rightConstraint = 960.0f;
float buffer = 1.0f; // set this so the spaceship disappears offscreen before re-appearing on other side
void Start()
{
// set Vector3 to ( camera left/right limits, spaceship Y, spaceship Z )
// this will find a world-space point that is relative to the screen
leftConstraint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * 0.05f,0.0f,0.0f)).x; // Or set to (0,0,0)
rightConstraint = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * 0.95f,0.0f,0.0f)).x; // Or set to (Screen.width,0,0)
}
void Update()
{
if (shipX < leftConstraint - buffer) { // ship is past world-space view / off screen
shipX = rightConstraint + buffer; // move ship to opposite side
}
if (shipX > rightConstraint + buffer) {
shipX = leftConstraint - buffer;
}
}