Be the first to post for 'Enemy Floating Health Bar'!

Great. I would like to figure out how to have the right analog control head movement along with mouse for head movement.

I thought it would be a good idea to post that I did figure out how to use both analog sticks on a controller.
Not very good at it yet.

i got it:

I am in a similar situation to Thomas, Do you remember which lecture Stephen that the rotating health bar issue is addressed in?

Not off the top of my head but it was a duplicate in setting the rotation of the healthbar.

My Enemy UI looks like this:

void Start()
{
    cameraToLookAt = Camera.main;
    Instantiate(enemyCanvasPrefab, transform.position, Quaternion.identity, transform);
}

// Update is called once per frame 
void LateUpdate()
{
    transform.LookAt(cameraToLookAt.transform);
}

I decided to go for circular health bar too, although I decided to use slider, to make it visible only on highlight and also when the player clicks on it, ie. focuses (locks).

Feel free to comment what should I improve further!

How did you make it rounded if you mind me asking? JDuchniewicz

New at this so Iam just throwing this out there. But do you have (2) HealthBar scrips within the project somewhere

I have that kinda of problem but my is where I am putting it at the feet and part of the bar disappears into the terrain.
Still trying to figure out how to correct it. Your problems seems like mine so if you figure out the fix let me know. Might be the angle it sits at because of the raycast is the center of the camera.

Hey, Sorry for the delay. The solution to making a circular bar like mine is as follows:

  1. Make a circular sprite and import it to Unity
  2. Add a Image to Enemy canvas, not RawImage, set the Image type to “filled”, and then fill method to “radial 360”.
  3. Add slider to it and modify/write your second HealthBar script where you update the bar’s progress

If I am unclear please say so :slight_smile:

1 Like

Hi,
This is my scene. All bars facing to camera. I’ve solved the facing problem by changing the Update method in EnemyUI script as follows.Needed to change Prefabs transform.
enemyCanvasPrefab.transform.LookAt(cameraToLookAt.transform);
enemyCanvasPrefab. transform.rotation = Quaternion.LookRotation(cameraToLookAt.transform.forward);

Cheers,


Really happy with this one :slight_smile:

4 Likes

Still follow the circular style :thinking:

1 Like


Finally got my health bars working! Nearly had a heart attack when my enemy’s health bar appeared about 20 meters wide. Luckily some re-scaling fixed that right up!

I have this error:
NullReferenceException: Object reference not set to an instance of an object
EnemyHealthBar.Update () (at Assets/Enemies/EnemyHealthBar.cs:22)

So I go to check in my scrypt clicking 2 times in the error
-----> float xValue = -(enemy.healthAsPercentage / 2f) - 0.5f;
… but I cant understand what is wrong here.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class EnemyHealthBar : MonoBehaviour
{
    RawImage healthBarRawImage;
    Enemy enemy;


    // Use this for initialization
    void Start()
    {
        enemy = GetComponentInParent<Enemy>(); // Different to way player's health bar finds player
        healthBarRawImage = GetComponent<RawImage>();
    }

    // Update is called once per frame
    void Update()
    {
        float xValue = -(enemy.healthAsPercentage / 2f) - 0.5f;
        healthBarRawImage.uvRect = new Rect(xValue, 0f, 0.5f, 1f);
    }
}


Thank you before all-

Domez

Hi Domez,

Do you have the Enemy.cs script component on the same GameObject that you have added the EnemyHealthBar.cs script component?

Hey, nice to meet you.

The big problem 's been when I had import all the full script of this lesson
(https://www.udemy.com/unityrpg/learn/v4/t/lecture/6688334?start=0)

So what happened? I had to refix all the scripts and now I had this problem.

Hmm I dont think to have it in the same place.



Maybe these screenshots can help better :confused:

1 Like

Hi,

You’re spot on, its not in the correct place.

Your code in the EnemyHealthBar script is looking for another component on the same GameObject of the type Enemy, e.g. your Enemy.cs script.

As you can see in your Hierarchy, your canvas has the EnemyHealthBar component but this isn’t part of your enemies.

If you add your EnemyCanvas to your enemy, along with the EnemyHealthBar script component it should then be able to find the Enemy component.

Quick question first though, are you following the course and adding the health bars to each enemy or, are you trying to add a UI where the enemy’s health is displayed, depending which enemy you are fighting?

Yes Im following the course but the problem started when I had imported all the scripts and they had reset :confused:

I tried to delete and change, but after all this chaos, I wrote here for have help directly :smiley:

Ok now I try to follow your tips.

1 Like

Ok, no problem. I only asked because if yowere doing something different to the course then my advice may have been wrong.

Don’t forget to update you enemy prefabs with the changes. Any problems, just reply :slight_smile:

Privacy & Terms