• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Persona · Oct 10, 2011 at 04:13 AM · positionscreenvectorspaceworld

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Veehmot · Jun 05, 2013 at 03:16 PM

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


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ryskill · Jan 30, 2015 at 11:44 PM 0
Share

This is awesome! Exactly what I was looking for!

avatar image
0

Answer by loopyllama · Oct 10, 2011 at 06:22 AM

 // 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..
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Persona · Oct 10, 2011 at 05:25 PM 0
Share

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);

avatar image loopyllama · Oct 10, 2011 at 08:05 PM 0
Share

mCamera.transform.position.z

avatar image Persona · Oct 12, 2011 at 03:51 AM 0
Share

Still no change.

avatar image loopyllama · Oct 12, 2011 at 04:20 AM 0
Share

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

avatar image Persona · Oct 12, 2011 at 06:14 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How spaces works in unity? 1 Answer

Up vectors are really in world space? 1 Answer

World to Screen 1 Answer

Screen point to point on Plane WITHOUT Raycast? 1 Answer

Particles in screen coordinates 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges