Hello developers.
Iam working on a Fullscreen-Radar like this in the picture below:
[Screenshot of the game “Freelancer” by Microsoft]
To get the screen-position of the target I use Camera.WorldToScreenPoint or Camera.WorldToViewPoint(). This works well until the target leaves the screen area.
However, I guess that these return-values are wrong if the target has an specific position.
Here is my Testcase:
[Top View of the testfield]
I have set up a testfield with 8 targets on differnt positions. The targets are the red cubes and the player is in the middle and his view goes up to the north (world.z+). The Y-Position of each cube are the same.
You see, there are targets on the left and right side of the player-view direction. The relative Z-Distance from camera to target is 0.
In this case Camera.WorldToScreenPoint and Camera.WorldToViewPoint() return 0,0,0.
Here is the log with the return values:
WRONG LABEL: 2nd objScreenPos is objViewPos:
[Debug.Log() of scrren- and viewPosition of the target]
WRONG LABEL: 2nd objScreenPos is objViewPos:
Please help me to understand this behaviour.
I expect that the viewpoint of the right target (EAST) is something of x= >1f and y= 0.5f. Y=0.5f, because the target.y pos is on the same level like the player.y pos. The player should only rotate to the right side to get the target in view (Without having to pitch down or up).
Thank you for reading to this point. Iam happy to read you suggestions.
Edit: Manual workaround before the usage of Camera.WorldToXPoint()
Here is my Workaround:
Vector3 objWorldPos = this.myTargets[i].transform.position;
//---
// 2. Camera.WorldToXPoint() Bugfixes:
float dotForward = Vector3.Dot(this.playerCamera.transform.forward, objWorldPos);
//Debug.Log("dotForward: " + dotForward);
if (dotForward == 0f) {
// Obj is left or right of player forward view.
// Translate object a little forward to avoid 0,0,0 return value of Camera.WorldToXPoint()
//Debug.Log("Object is on Left/Right side of player forward view and will be moved temporary");
objWorldPos.z += 0.01f;
}
if (Vector3.Dot(this.playerCamera.transform.up, objWorldPos) == 0f) {
// Obj is top or down of player forward view.
// Translate object a little up to avoid 0,0,0 return value of Camera.WorldToXPoint()
//Debug.Log("Object is on top/bottom side of player forward view and will be moved temporary");
objWorldPos.y += 0.01f;
}
if (dotForward > 0f && Vector3.Dot(this.playerCamera.transform.right, objWorldPos) == 0f) {
// Obj is behind of player forward view.
// Translate object a little to the right to get an angle where the arrow will look at (if objs is behind camera).
//Debug.Log("Object is on back side of player forward view and will be moved temporary");
objWorldPos.x += 0.01f;
}
After that workaround Camara.WorldToXPoint() does not return 0,0,0 anymore.