Author Topic: Offscreen object indicator [Almost solved]  (Read 3438 times)

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Offscreen object indicator [Almost solved]
« on: October 01, 2015, 06:48:20 AM »
Hi I am trying to get offscreen object indicators to work...I know how to position them on the edge of the screen but I am not able to figure out how to rotate the indicator so it is both placed on the edge of the screen and at the same time points in the direction of the object that is offscreen.

Here is what I have:

Update loop


  1. //Converting to viewport point because it's easier to find point on the edge of the screen
  2. var worldToViewportPoint = _gameCamera.WorldToViewportPoint(Target.position);
  3. //Convert player position to screen point (this is constant because player is always centered, it is linked to game camera)
  4. var playerWorldToScreenPoint = _gameCamera.WorldToScreenPoint(GameController.Instance.Player.CharacterTransform.position);
  5.  
  6. //Clamp the target position to edge of the screen (take GetPixelSizeAdjustment  into account since i am not using pixel perfect)
  7. var screenPosClamped = new Vector3(Mathf.Clamp(worldToViewportPoint.x, 0f, 1f),Mathf.Clamp(worldToViewportPoint.y, 0f, 1f), 0);
  8. var position = new Vector3((screenPosClamped.x * Screen.width - HalfScreenWidth) * UIRoot.GetPixelSizeAdjustment(_indicator.gameObject),
  9.                                         (screenPosClamped.y * Screen.height - HalfScreenHeight) * UIRoot.GetPixelSizeAdjustment(_indicator.gameObject), 0);
  10.  
  11. //This gives me for one point something like this: (427.0, 191.6, 8.7) (-455.1, 360.0, 0.0)
  12. //So I suspect this is something that is causing trouble
  13. Debug.Log(playerWorldToScreenPoint.ToString() + " " + position.ToString());
  14.  
  15. //Set indicator prefab position at the edge of the screen (THIS IS WORKING CORRECTLY
  16. _indicator.localPosition = position;
  17.  

My question is what I need to do so that indicator rotates towards the player? I tried all kinds of combinations looking at the direction from those two points and
using LookAt and Slerp but no solution gives me good result. What is the proper way of doing this?

Thanks!


pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Re: Offscreen object indicator [Almost solved]
« Reply #1 on: October 02, 2015, 02:16:19 AM »
I got to bump this, I need some help

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Offscreen object indicator [Almost solved]
« Reply #2 on: October 03, 2015, 06:35:55 PM »
This is a math question, not an NGUI question... But, off the top of my head:

Find the viewport position:
  1. Vector3 pos = gameCamera.WorldToViewportPoint(obj.transform.position);
Figure out if it's in front or behind the camera (if using Unity 4, change "orthographic" to "isOrthographic"):
  1. int isVisible = (gameCamera.orthographic || pos.z > 0f) && (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f) ? 1 : 0;
  2.  
You can use the "isVisible" flag to hide the widget if it's visible, since you mentioned you needed off-screen indicators. Moving on, clamp the position in visible viewport range:
  1. pos.x = Mathf.Clamp01(pos.x);
  2. pos.y = Mathf.Clamp01(pos.y);
Last step is to transform to the widget coordinates:
  1. pos = uiCamera.ViewportToWorldPoint(pos);
  2. pos = widget.transform.parent.InverseTransformPoint(pos);
  3. pos.z = 0f;
  4. widget.transform.localPosition = pos;
All this code was pretty much copy-paste from NGUI HUDText extension.