x


Keeping the player inside the screen?

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.

more ▼

asked Oct 10 '11 at 04:13 AM

Persona gravatar image

Persona
261 76 85 97

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
// 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..
more ▼

answered Oct 10 '11 at 06:22 AM

loopyllama gravatar image

loopyllama
1.5k 1 4 18

NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)

That error appears when I use this in JS:

var camToObject: float = Mathf.Abs(mCamera.position.z - transform.position.z);

Oct 10 '11 at 05:25 PM Persona

mCamera.transform.position.z

Oct 10 '11 at 08:05 PM loopyllama

Still no change.

Oct 12 '11 at 03:51 AM Persona

mCamera has to be set to an instance of a camera.

Oct 12 '11 at 04:20 AM loopyllama

function Awake(){ //Set the main camera mCamera = Camera.main; }

Oct 12 '11 at 06:14 PM Persona
(comments are locked)
10|3000 characters needed characters left

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);
    }
}
more ▼

answered Jun 05 at 03:16 PM

Veehmot gravatar image

Veehmot
542 14 20 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x915
x502
x205
x186
x115

asked: Oct 10 '11 at 04:13 AM

Seen: 1229 times

Last Updated: Jun 05 at 03:17 PM