Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by stephen_mmm · Apr 09, 2017 at 05:29 AM · rectscreentoworldpointorthographic cameraworldtoscreenpointperspective camera

Objects overlapping in perspective rather than real space?

Hi,

I'm currently working on a project which mixes 2D elements (sprites, orthographic camera) with 3D ones (camera moves in 3D space when tracking), in a way which frequently ends up creating some perspective tricks - the player object could look on the screen as if it's overlapping another, depending on camera position and rotation, but in world space the two are actually far apart.

My question is- how to flag when this overlap happens, on the screen rather than in world space? I've been paying with various combinations of converting from WorldToScreenPoint and then back using ScreenToWorldPoint, in the hopes this will give me world coordinates for where it LOOKS like these objects are, but so far the latter just sends me back to the actual places that these objects are rather than where they look like they are on the screen.

I've tried just comparing screen position to screen position, but am using rect.contains for the actual overlap and this uses world space.

I've attached my testing code below but it doesn't give me what I need, if anyone has any clues on what I'm missing it would be greatly appreciated!

 //Testing when player overlaps transform in viewport
 
 var viewPos: Vector3;
 var playerPos: Vector3;
 
 var myRectX : float = 0.4;
 var myRectY : float = 0.5;
 
 var rect: Rect;
 
  var viewPosWorld : Vector3;
 var playerPosWorld : Vector3; 
 
 function Update () {
 
 viewPos = camera.main.WorldToScreenPoint(transform.position);
 
 var myPlayer : GameObject = gameObject.FindWithTag("Player");
 playerPos = camera.main.WorldToScreenPoint(myPlayer.transform.position);
 
 viewPosWorld = camera.main.ScreenToWorldPoint(Vector3(viewPos.x,viewPos.y,0));
 playerPosWorld = camera.main.ScreenToWorldPoint(Vector3(playerPos.x,playerPos.y,0));
 
 rect = Rect(viewPosWorld.x, viewPosWorld.y, myRectX, myRectY);
 
     
 if (rect.Contains(playerPosWorld))
 {
 Debug.Log("Beep");
 }
 }
 
 function OnDrawGizmos()
 {
 Gizmos.color = Color.white;
 Gizmos.DrawWireCube (Vector3(rect.x, rect.y,0), Vector3 (myRectX,myRectY,1));
 Gizmos.DrawWireCube (playerPosWorld, Vector3 (myRectX,myRectY,1));
 }

Thank you! Stephen

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

1 Reply

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

Answer by stephen_mmm · Apr 16, 2017 at 12:58 PM

For the curious, here's a method I'm using now which seems to fix the issue by using spherecasts instead:

 #pragma strict
 
 var myNewScene : String;
 
 function Update () {
 var hit : RaycastHit;
 
 var viewPos: Vector3;
 viewPos = camera.main.WorldToScreenPoint(transform.position);
 var ray: Ray = camera.main.ScreenPointToRay(viewPos);
 
 if (Physics.SphereCast(ray.origin,0.5,ray.direction, hit))
 {
 if (hit.collider.gameObject.tag == "Player")
 {
 //Debug.Log("Beep");
 Application.LoadLevel(myNewScene);
 }
 }
 Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
 }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Comparing Orthographic and Perspective Cameras 1 Answer

Aquarium like camera 1 Answer

How can touch input directly control an orthographic camera? 1 Answer

[Solved]Orthographic camera RECT based off points on screen 1 Answer

How do I implement orthographic elements into a perspective camera? 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