- Home /
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
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612115000im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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