For various reasons, I’m trying to keep a 3d object inside the screen, no matter how the player resize it, so I’m trying to make a code for it. But no matter what it doesn’t seem to reach zero.
//Set the main Camera
var mCamera: Camera;
//Set up how far back it will be pushed
var yCounter: float = 10.0f;
//Set the lives to private so that it can only be changed in script
private var lives: int = 3;
function Awake(){
//Set the main camera
mCamera = Camera.main;
//Set the player's starting position
//transform.position.y = Screen.height/2;
}
function Update (){
///Movement Limitations
/*
We have to set up limits as to how far the player can travel of course.
Otherwise we'd lose track of it.
However, trying Screen.height and Screen.width don't work on 3d objects and space, so we have to convert it.
So this snippet of coding will stop it from going above or below the screen.
*/
// Use the following to convert from Screen Point to World Point
var horizontalBorder : Vector3 = mCamera.ScreenToWorldPoint(Vector3 (0,Screen.height,-5));
var horizontalBorder2 : Vector3 = mCamera.ScreenToWorldPoint(Vector3 (0,0,-5));
//Now set up the return from the border
var horizontalReturn: Vector3 = (Vector3(0, yCounter, 0));
//Lastly set a distance that must be maintained
var dist = Vector3.Distance(horizontalBorder, transform.position);
var dist2 = Vector3.Distance(horizontalBorder2, transform.position);
print(dist);
if (dist < 0) {
transform.position = Vector3.zero;
}
else if(dist2 < 0) {
transform.position = Vector3.zero;
}
}
Originally I was trying to just get the Screen.height as a float to compare with the transform.position.y, but I couldn’t figure out how, and the coding got more complex. Normally, I’d just stick a invisible wall at the bottom of the screen, but that wouldn’t fly this time.
The problem is that you are not checking against the bounds of the object. Each object in Unity has a bounds property that tells you exactly the space occupied by that object. You need to use the min and max values to compare them against the edges of the screen, and correct the position.
Also, be sure to perform the checks in LateUpdate instead of Update, to ensure that all movement functions have finished.
I have dealt with this issue before for a 2D game, so I can provide a component I made that restricts the movement to the screen.
/**
* Restricts the movement of the object to the screen.
* @author Jorjon
*/
using UnityEngine;
public class Boundings2D : MonoBehaviour {
void LateUpdate () {
var left = Camera.main.ViewportToWorldPoint(Vector3.zero).x;
var right = Camera.main.ViewportToWorldPoint(Vector3.one).x;
var top = Camera.main.ViewportToWorldPoint(Vector3.zero).y;
var bottom = Camera.main.ViewportToWorldPoint(Vector3.one).y;
float x = transform.position.x, y = transform.position.y;
if (transform.position.x <= left + renderer.bounds.extents.x) {
x = left + renderer.bounds.extents.x;
} else if (transform.position.x >= right - renderer.bounds.extents.x) {
x = right - renderer.bounds.extents.x;
}
if (transform.position.y <= top + renderer.bounds.extents.y) {
y = top + renderer.bounds.extents.y;
} else if (transform.position.y >= bottom - renderer.bounds.extents.y) {
y = bottom - renderer.bounds.extents.y;
}
transform.position = new Vector3(x, y, transform.position.z);
}
}
// you are forgetting that the z in screen to world point is very important
// below is c sharp but you can easily translate to javascript
float camToObjZ = Mathf.Abs (Camera.main.position.z - objT.position.z);
Vector3 horizontalMin = Camera.main.ScreenToWorldPoint (new Vector3 (0,0,camToObjZ);
Vector3 horizontalMax = Camera.main.ScreenToWorldPoint (new Vector3 (screen.width,0,camToObjZ);
if (objT.position.x < horizontalMin)
objT.position.x = horizontalMin;
//etc..