0 votes

I have this Spatial node called Life where I have the viewport containing a TextureRect and a ColorRect to construct the lifebar. Finally, a Sprite3D to render the 2D image into the 3D world.

structure

In the life script I have the following code to update the size of the lifebar according to the unit's max health:

func calculate_size(value):
    return Vector2(32 * value / 50.0, 32)

func set_max_life(value):
    total_life = max(0, value)
    current_life = total_life
    var size = calculate_size(total_life)
    $Viewport.size = size
    $Viewport/LifeBox.rect_size = size
    $Viewport/BarBox.rect_size = size

Then I have another 3D scene called Unit, that has Life as a child. Unit has the following script:

func _ready():
    var max_life = 100
    $Life.set_max_life(max_life)

Then, in my main scene I have one unit instantiated in the 3D editor, and another one through the following GDScript:

func _ready():
    var nav = get_parent().get_parent()  # Navigation Node
    var unit_scene = preload("res://Unit.tscn")
    for index in range(3):
        var unit = unit_scene.instance()
        var position = -(index + 1)
        unit.translate(Vector3(position, .2, position))
        unit.rotate_y(135 * PI / 180)
        nav.call_deferred("add_child", unit)

But then, only the unit instantiated through the editor seems to have the lifebar size updated:

error

What am I doing wrong?

Godot version 3.2.2
in Engine by (17 points)
edited by

1 Answer

+1 vote

I think you're going about this the wrong way... my understanding is that having more viewports is not great for performance...

You might be better of have a quad that's draw at the location, a rotated to face the camera (aka a billboard). is there a reason you're not using 3d Sprite or something like that?

They talk about billboards here:
https://godotengine.org/qa/30238/how-to-make-a-3d-sprite-always-look-at-the-player

by (1,290 points)

I think it's easier to make health bars using 2D nodes. Using only 3D sprites would demand a lot of coding (I suppose).

Every tutorial I saw about making 2D health bars in a 3D environment used viewports, which is the only reason I used it. But I guess you are right, they look expensive.

I'll try moving on to using only 3D sprites then. Thanks for the reply!

I might be wrong about this though! if it's working for you and it's not using an unexpected amount of resources, then maybe viewports is the right way to go... i hadn't come across it until now!

they are being harder to use than I expected, i'll give a solo sprite3d a try anyways :)
thanks again!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.