Author Topic: Tracking and Pointing Towards Off-Screen Enemies  (Read 5449 times)

danthat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Tracking and Pointing Towards Off-Screen Enemies
« on: November 10, 2012, 08:46:50 AM »
I'd like to use nGUI to point the player towards any off-screen enemies. So if an enemy is 400 metres to the right of the player, you'd get a little enemy icon pointing in his general direction. Terrible mockup screenshot example:



I can't quite get my head around the best way to do it using nGUI. Should I be using HUDtext for this, or is it all possible with nGUI vanilla?

Problem is, I'm unsure how to generate widgets at runtime with nGUI. I can grab all Colliders in range of the player with Physics.OverlapSphere and test Renderer.IsVisible... but I then need a way of generating widgets and clamping the labels to screen extremities.

Ideally I guess all Enemy objects would just have a child widget that I send to my Panel if !Renderer.IsVisible, but that doesn't seem workable because I think widgets require a Panel parent? Is that right?

Any suggestions gratefully received. I thought this sort of thing would be widely discussed, but I've been Googling around the subject for a couple of hours and haven't found a decent solution.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tracking and Pointing Towards Off-Screen Enemies
« Reply #1 on: November 10, 2012, 11:42:19 AM »
HUDText won't help you here. Knowing the position of your enemy, you can simply (enemyPos - yourPos) and normalize it to get the direction. This gives you a value of XY in -1 to 1 range. Multiply X by half of screen width, and Y by half of screen height, and you get your position on the screen's edge where you will place your target indicator.

danthat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Tracking and Pointing Towards Off-Screen Enemies
« Reply #2 on: November 10, 2012, 02:59:40 PM »
Ah, ok. Thanks. And what about generating suitable widgets at runtime? Is there a sensible method behind that?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Tracking and Pointing Towards Off-Screen Enemies
« Reply #3 on: November 11, 2012, 11:00:09 AM »
I recommend creating a prefab out of what a single indicator should be like, then simply instantiating it using NGUITools.AddChild.