Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Aladine · Aug 26, 2013 at 09:20 PM · scalescreenunit

get screen width and height in world size

Hello everyone,

if i have a game with a resolution 800x600, how to make a condition to check if some object is out of the screen ? in other term i want to convert the 800 and 600 to the transform unit (basically it's always a small number) i tried doing this : print (Camera.main.ScreenToWorldPoint(new Vector3(800,600,0))); but the result is always : (0.0, -0.1, -2.8)

thank you

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Aladine · Aug 27, 2013 at 02:23 PM

well, am feeling very bad now :p i should never code when i need to sleep, here is what i need :

 Vector3 tmpPos = Camera.main.WorldToScreenPoint (transform.position);
                 if (tmpPos.x > Screen.width ) {
                     Debug.Log ("Destroy");
                     Destroy(gameObject);
                 }

i don't know why i kept asking for screen conversion, thank you all

Comment
Add comment · Show 2 · 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 ismaelnascimento01 · Feb 17, 2018 at 02:27 AM 0
Share

Thank you !

avatar image ElijahShadbolt · Feb 17, 2018 at 05:18 AM 0
Share

You might also want a $$anonymous$$imum value check.

 if (tmpPos.x > Screen.width
     || tmpPos.x < 0f
     || tmpPos.y > Screen.height
     || tmpPos.y < 0f)
 {
     ...
 }
avatar image
3

Answer by Ejlersen · Aug 26, 2013 at 09:25 PM

Hm, if you just want to check if the object is visible to the camera, then you can use:

MonoBehaviour.OnBecameVisible() http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameVisible.html

MonoBehaviour.OnBecameInvisible() http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnBecameInvisible.html

You can also use:

GeometryUtility.TestPlanesAABB(...) http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.TestPlanesAABB.html

with: GeometryUtility.CalculateFrustumPlanes http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html

Then you can test the objects bounding box against the camera frustrum.

Comment
Add comment · Show 4 · 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 Aladine · Aug 27, 2013 at 08:21 AM 0
Share

thank you, but i tried that and it gives me strange results, i did a log check for onBecameVisible and invisible and it print visible when the object is still not in the scene, and "invisible" after the object is far away from the scene, any suggestion ?

avatar image robertbu · Aug 27, 2013 at 11:14 AM 0
Share

OnBecameVisible() and OnBecameInvisible() work with all cameras. In the editor, that includes the scene cameara, so turn your scene camera away from the action if you can and rerun the test. They also test against the Renderer.bounds, so they can return true before an object is visible.

If your game is 2D, you can convert your object to Viewport space (Camera.WorldToViewportPoint(). A position is visible if the x and y values of the returned point are in the range of (0 to 1). You will need to increase the range slightly to account for the width and height of the object.

avatar image Aladine · Aug 27, 2013 at 02:04 PM 0
Share

@robertbu

OnBecameVisible() and OnBecameInvisible() work with all cameras. In the editor, that includes the scene cameara, so turn your scene camera away from the action if you can

i don't know what do you mean by scene camera, i only have one camera, the default one.

and here is what i actually want to do, it kinda "works" but it doesn't give me the right result when i want to apply it to the screen size,

     print ("Standard" + myTransform.position.x);
         
             Vector3 worldToScreen = Camera.main.WorldToScreenPoint (myTransform.position);
             print ("World" + worldToScreen.x);
             
             Vector3 screenToWorld = Camera.main.ScreenToWorldPoint(worldToScreen);
             print ("Screen" + screenToWorld.x );
     

the ScreenToWorld.x is always equal to myTransform.position.x which is what i want to do (the point is converting screen value to world value)

now when i try this :

  Vector3 myScreen = new Vector3(Screen.width,Screen.height,0);
             print ("screen"+ myScreen.x);
             
             Vector3 myWorld = Camera.main.ScreenToWorldPoint(myScreen);
             print ("myWorld"+ myWorld.x);
         

the second print always print "0" i tried ScreenToViewPortPoint and the result is always either "1" or "0.9996684" (i track the value by changing the game window size during the play) thank you

avatar image Ejlersen · Sep 01, 2013 at 07:14 AM 0
Share

When in the Unity editor, you have two cameras. The "Ingame $$anonymous$$ain Camera" and the "Editor Scene Camera" (the one you use to edit your scene with).

When using ScreenToWorldPoint, remember that the z-value is distance from camera. So giving a distance of 0 will likely give something resembling the cameras own position.

You could calculate the distance to your object, e.g. (obj.transform.position - Camera.main.transform.position).magnitude.

avatar image
0
Wiki

Answer by twobob · Sep 29, 2014 at 11:20 PM

visualising screen space, checking for occlusion

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// Update camera near plane world coords.
 /// This could be repurposed to handle camera occlusion 
 /// Directing some event (such as moving the camera) to occur 
 /// rather than the debug messages.
 /// </summary>
 public class UpdateCameraNearPlaneWorldCoords : MonoBehaviour {
 
     // properties
     public Camera theCamera;
     public GameObject player;
 
     Vector3 checkpoint; // player transfrom that we offset from 
     Bounds bounds; // player bounds for positioning sensible check point.
     Vector3    p; // point in space
     Vector3 trajectoryEndPoint; // where we want to check we can see in world space
     Vector3 trajectoryStartPoint; // The "checkpoint" with a useful offset.
     Vector3 trajectory; // the direction of the ray to fire.  ( I invert it here to be fair)
 
     static RaycastHit hit;  // Stop it needing to be declared a bazzillion times.
 
     // Helper Method
     static bool CheckIsOccluded (Vector3 trajectoryStartPoint, Vector3 trajectory, Vector3 trajectoryEndPoint)
     {
         // Simply return true or false.  True means we are occluded.
         // We could have used...
         //return Physics.Raycast (trajectoryStartPoint, -trajectory, out hit, Vector3.Distance (trajectoryStartPoint, trajectoryEndPoint));

         //but this makes more sense in this instance
         return Physics.Linecast(trajectoryStartPoint, trajectoryEndPoint, out hit);
     }
 
     // Use this for initialization if you are networked or something
     void Start () {
 
         // Could find objects dynamically is we are running NetworkView stuff. via isMine.
         // In this example The PLAYER and CAMERA are simply dragged on to get the reference
 
         // You might have a mesh filter or w/e - get the right one from the right level (Self/Child).
         bounds = player.transform.gameObject.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh.bounds;
 
 
     }
 
     void SetupValuesReadyForCornerCheck (float x, float y)
     {
         p = theCamera.ScreenToWorldPoint (new Vector3 (x, y, theCamera.nearClipPlane));
         trajectoryEndPoint = p;
         trajectory = trajectoryStartPoint - trajectoryEndPoint;
         Debug.DrawLine (trajectoryStartPoint, trajectoryEndPoint, Color.red);
     }
     
     void Update () {
 
         checkpoint = player.transform.position;
 
         // Make this be somewhere useful to you. This place is about right for me.
         trajectoryStartPoint = (checkpoint - bounds.center) + (Vector3.up *0.4f) ;
 
 
 // TOP LEFT
 
         SetupValuesReadyForCornerCheck (0, Screen.height);
 
         if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
             //Vector3 targetPosition = hit.point;
             Debug.Log("Occluded Top Left");
         }
 
 // BOTTOM LEFT
 
         SetupValuesReadyForCornerCheck (0, 0);
 
         // Check for hit.
 
         if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
                         //Vector3 targetPosition = hit.point;
                         Debug.Log ("Occluded Bottom Left");
                 }
 
 // TOP RIGHT
 
         SetupValuesReadyForCornerCheck (Screen.width, Screen.height);
 
         // Check for hit.
         
         if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
             Debug.Log("Occluded Top Right");
         }
 
 // BOTTOM RIGHT
 
         SetupValuesReadyForCornerCheck (Screen.width,0);
 
         // Check for hit.
         
         if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
             //Vector3 targetPosition = hit.point;
             Debug.Log("Occluded Bottom Right");
         }
 
 
     }
 }
 

Is not far off the mark. Could doubtless be more efficient.

EDIT: if you want to see what is happening drop in a

 Debug.DrawLine(trajectoryStartPoint, trajectoryEndPoint, Color.red);

Inside the helper occlusion check. (pictured, with some extra GUI texts for clarity)

Elephants, awesome


shownearplanecorners.jpg (104.2 kB)
debugnearplane.jpg (88.7 kB)
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 twobob · Sep 30, 2014 at 12:12 AM 0
Share

Just to add. if one really wanted to use this for probing occlusion, a 5th "$$anonymous$$IDDLE" probe with some extra depth would be helpful.

 // $$anonymous$$IDDLE
 
 
         p = theCamera.ScreenToWorldPoint (
             new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, theCamera.nearClipPlane)
             );
 
     //    trajectoryEndPoint = p;
 
         trajectory = trajectoryStartPoint - p;
 
 
         float distanceApart = Vector3.Distance (player.transform.position,p);
 
 
         trajectoryEndPoint = trajectoryStartPoint +  -trajectory * 1.05f ;
 
 
 
         Debug.DrawLine (trajectoryStartPoint, trajectoryEndPoint, Color.red);
 
 
         // Check for hit.
         
         if (CheckIsOccluded (trajectoryStartPoint, trajectory, trajectoryEndPoint)) {
             //Vector3 targetPosition = hit.point;
             Debug.Log("Occluded Center screen");
         }
 

Depth Probe

depthprobe.jpg (97.7 kB)

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scale objects to fit screen unity 0 Answers

How to get an object to fill 3/4th of the screen. 0 Answers

Unity unit scale compared to Maya 1 Answer

How to scale everything with screen size? 4 Answers

WebGL output scale to fit browser window even when window changes aspect ratio 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges