playMaker

Author Topic: [SOLVED] Another health bar thread  (Read 17684 times)

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
[SOLVED] Another health bar thread
« on: October 09, 2012, 01:09:42 PM »
After reading all the threads I could find on this I still find no solution, thought i'd ask.
 
I'm trying to recreate what happens in this script with playmaker - heres a video tutorial : http://www.youtube.com/watch?v=iER_LFwi1-U
 
I could post the whole thing if required but the important bits of code are:  
 
Code: [Select]
energyBarInstance.transform.position= ThisCamera.WorldToViewportPoint (transform.position);
energyBarInstance.transform.position.x -= ebOffsetX;
energyBarInstance.transform.position.y -= ebOffsetY;
energyBarInstance.transform.localScale = Vector3.zero;

 
This lets you place your unity gui object basically wherever you want with an offset (very handy), but mainly its about a prefab gui texture floating above an object of your choosing without any parenting  
 
Code: [Select]
energyBarWidth = energyPercent * 20;
energyBarInstance.guiTexture.pixelInset = Rect(10,10,energyBarWidth,5);

 
This lets you dynamically scale/size the gui texture on the prefab object very precisely.
 
So my attempts to recreate this in playmaker have left me miffed so far and I have not found a resource that does anything similar - closest being http://hutonggames.com/playmakerforum/index.php?topic=2376.0 that ends up with mucking around with boxes (big no no).
 
I've found some stuff on pixelInset but I can find no reference to WorldToViewportPoint in any playmaker resources so its probably safe to say I'm trying to recreate the technique using the wrong methods, I hope someone can set me straight about this.
« Last Edit: October 11, 2012, 09:42:19 AM by krakov73 »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Another health bar thread
« Reply #1 on: October 09, 2012, 01:31:15 PM »
Hey there, you want to use GUIs to show the health bars above each enemy, right? Then since you seem to know what you're doing, here's a couple of things to hopefully get you up and running. Do ask if you need more detailled info.

Quote
I've found some stuff on pixelInset but I can find no reference to WorldToViewportPoint in any playmaker resources so its probably safe to say I'm trying to recreate the technique using the wrong methods, I hope someone can set me straight about this.
"screen to world space" - "world to screen space"

So you can use the world to screen space for the X/Y coordinates of your GUI. Then the offset could be the position of the target + a certain value on the Y axis, and then all that in screen space.

So in the same state as the GUI, get both the camera's position each frame, and the target position each frame. Add a "Is Visible" action to make sure only the visible stuff is called each frame. That Is Visible action could be called every second or so to save performance.

Then use get Distance to get the distance between the camera and target to use in a float operator to rescale the size (farther away = smaller gui).

That should be all.

Like I said, if you need more details, say so, I'm somewhat in a hurry right now, but this is a nice topic.
But do tell, why no mesh bars?! They would be the best choice for this both performance and effort wise. All you'd need is to use a look toward action to make them always face the camera.
« Last Edit: October 09, 2012, 01:33:31 PM by kiriri »
Best,
Sven

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Another health bar thread
« Reply #2 on: October 09, 2012, 01:46:31 PM »
Thanks for jumping in kiriri yes I do want to populate health bars everywhere :P

I have been trying to use world to screen space but no luck so far - I think I'm getting confused with the prefab GO using a normal vector3 & its gui texture component using vector2...I'll have another bash at it :)

My scene has a bunch of dynamic moving cameras but I only need this gui stuff visible on one of them, my research on this has mentioned that playmaker has some sort of relationship with a designated "main camera". Could that be an issue thats stopping my gui texture showing up?

ps: I absolutely do not want these gui elements scaling in 3d ... i need to keep their size on screen predictable.
« Last Edit: October 09, 2012, 01:49:53 PM by krakov73 »

Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • my twitter @andrewlukasik
Re: Another health bar thread
« Reply #3 on: October 09, 2012, 01:56:35 PM »
Hi krakov73,
I would second what @kiri said. 3d bars are probably easiest solution (predictable size is only a question of math or way you design such system). With layers you can make them rendered always on top of other geometry, so that's not a problem also.

Just a check: did you used "Enable Gui" action?
« Last Edit: October 09, 2012, 02:13:03 PM by Andrew_Raphael_Lukasik »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Another health bar thread
« Reply #4 on: October 09, 2012, 02:09:58 PM »
did you make sure to normalize the screen position?
Best,
Sven

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Another health bar thread
« Reply #5 on: October 09, 2012, 02:12:45 PM »
Hi krakov73,
Just a check: did you used "Enable Gui" action?

Imo 3d bars are probably easiest solution (predictable size is only a question of math).

Using mesh bars instead of textures seems to be the emerging consensus...i'll report back after i've worked it all through some more...

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Another health bar thread
« Reply #6 on: October 09, 2012, 02:16:49 PM »
www.twisted-pantheon.com/Downloads/New%20Unity%20Project%208.7z

gee, you tried like 5 minutes :P Playmaker is all about being headstrong ;)
Add your playmaker.dll yourself (this needs to be done to protect ones license)
Best,
Sven

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Another health bar thread
« Reply #7 on: October 09, 2012, 07:28:00 PM »
Thanks for the project file kiriri!

After some messing around I've got your example working in my scene.

Unfortunately its not producing the desired effect, as it only seems to work in the X/Z planes - currently when the camera or target moves up the gui object moves down & vise versa - there is probably a super simple solution to that like inverting the screen Y value but I need sleep so I'll mess with that tommorow.

Still nice to have an example closer to the scripted gui solution, so i'll hold off on using physical sprites for a bit longer.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Another health bar thread
« Reply #8 on: October 10, 2012, 01:34:53 AM »
Hi,

 have a look at this sample, it's stamina bar but really also a health bar depending on how you control it.

http://hutonggames.com/playmakerforum/index.php?topic=2376.msg10550#msg10550

bye,

 Jean

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Another health bar thread
« Reply #9 on: October 10, 2012, 06:50:03 AM »
Thanks to those who have chipped in so far but it seems like playmaker does not have the commands neccessary to reproduce my script ie - WorldToViewportPoint.

jeanfabre  - please read the op (I've already mentioned your method).

After some experiments I'm not going to be happy using meshes, it just does not look the same, especially in motion where it looks like your actors have boxes floating above them instead of the specific gui overlay look that the gui system provides. As Mr Lukasik  mentioned "predictable size is only a question of math" but I'm currently short on maths degrees and he did not provide his working formula.

I'm inclined to call my script from the fsm because it just works & is only a few lines, but playmakers lack of gui abilities seems to me to be the reason everyone uses meshes as a workaround, not because its better that way (happy to be proved wrong though, not being snarky).

Maybe this will all change when/if unity get its new gui system.

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Another health bar thread
« Reply #10 on: October 10, 2012, 10:37:37 AM »
if all you need for my project to work is to invert the screen Y, that would be the difference of screen Y. A difference is a term for 1-Value . So The difference of screen Y would be calculated in a float operator as 1-screenY = screen Y (this is a math term, so coders don't complain!)
I'll update the project in a min.
Best,
Sven

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Another health bar thread
« Reply #11 on: October 10, 2012, 10:47:59 AM »
if all you need for my project to work is to invert the screen Y, that would be the difference of screen Y. A difference is a term for 1-Value . So The difference of screen Y would be calculated in a float operator as 1-screenY = screen Y (this is a math term, so coders don't complain!)
I'll update the project in a min.

Yes I'm actually getting good results with that extra float operator. Also it gives me the verticle offset i can use to stack several items over an object. Sleep really helps brain work :)

Do make that example for the masses as anyone learning unity script will have come across the method in the op.

I'll now be trying to give the object a dynamic width so i can have it charging up & down.

I think this ones almost solved apart me finishing the job. gg!
« Last Edit: October 10, 2012, 10:49:30 AM by krakov73 »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Another health bar thread
« Reply #12 on: October 10, 2012, 10:57:44 AM »
Ok, I'll do this one for the wiki then. I've updated the previous link, so incase it doesn't work look there.

The only thing left is to move the pivot point of the bar to its' middle. Ehhh just google that, I think I explained that to someone just yesterday or so... or wait for me to finish up this example, though I'll do that tomorrow, since I'm a bit busy with my own projects.
Best,
Sven

Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • my twitter @andrewlukasik
working health bar _ example _ unity package _ download
« Reply #13 on: October 10, 2012, 11:00:06 AM »

gui health bar boxes 101


I'm not sure if it is what you have been looking for @krakov73 but explaining this solution looked like taking more time that actually sharing it done.

PS: Depending on project this can require optimizations etc. but it demonstrates the idea and simple aligning/scaling algorithm.
 If you want to modify it I recommend trying @jeanfabre's billboard action.

A.
« Last Edit: October 11, 2012, 06:13:10 AM by Andrew_Raphael_Lukasik »

krakov73

  • Playmaker Newbie
  • *
  • Posts: 28
Re: working health bar _ example _ unity package _ download
« Reply #14 on: October 10, 2012, 11:30:43 AM »
gui health bar boxes 101
...

Explaining this solution looked like taking more time that actually sharing it done.
I hope it will help somebody.

A.

I think you just pwned the thread.

I'm looking through it now and...it works just like I want it to  :o

fantastic, cigars all round.