Well done, Heath!
I’ve used your logic and created a slider for Player health and Level but i’m facing a problem with enemy health bar , i tried to add the health bar above the enemy and it works but now when i try to setActive(false) the canvas. it disappear in all enemies from the same type prefab. and when i attack. it appears in all enemies again, even the dead ones. how can i make the canvas disappear only for the dead enemy and the same for show canvas.
PS: i use unity assets for Player health bar UI.
hmmm… would need to see your code for the script responsible for turning those on and off
Hi!
What I higly suggest in this case is to use Animations instead of code, this allows for many cool features that would be otherwise pretty hard to control with code, here’s an example of what I mean:
In this quick thing I did, you can clearly see that the Health Bars react individually and even have a cool fade at the end. This can be easily done with the Animator and one simple line of code which is this.
GetComponent<Animator>().Play(nameOfTheState, 1, 0);
This implies that if you hit the enemy multiple times the animation will reset, instead of having 20 timers in your code, you can control the time with Animations, meaning that tweaking the timer is extremely more easy since the only thing you’ll have to do is modify the animation by moving a simple slider instead of going into the code or setting each timer individually.
One thing is worth mentioning is that in the video the transitions are overcomplicated, they can work just fine with this setting:
You don’t even need parameters for this to work, just exit transitions.
Remember you’ll have to add a second layer to the Animator with this settings:
This might see a little convoluted at first, but believe me, the less code you have the better, and Unity allows to do many cool things without writing a single line of code or in this case, one line of code.
what an interesting approach… that’s neat…
Not sure what the approach your code is taking to turn the bars on/off… they should be individual in the first place, and it sounds like your script is turning it on/off in the prefab?
Making sure you’re not referencing the prefab, that you’re referencing the slider on the actual enemy, simply Destroy() the slider.
yes i have tried to destroy the slider and it destroy it on all enemy prefab, it like you said. maybe i’m referencing all enemy prefab. i’ll share the code in a sec
Yeah, most likely you’re referencing the Prefab, not the instance
Billboard.cs
using RPG.Stats;
using UnityEngine;
namespace RPG.Core
{
public class Billboard : MonoBehaviour
{
public Transform cam;
public GameObject healthBar = null;
Fighter fighter;
private void Awake()
{
fighter = GameObject.FindWithTag("Player").GetComponent<Fighter>();
}
private void LateUpdate()
{
transform.LookAt(transform.position + cam.forward);
if (fighter.GetTarget != null)
{
Health health = fighter.GetTarget;
if (health.IsDead())
{
healthBar.gameObject.SetActive(false);
}
else if (!health.IsDead())
{
healthBar.gameObject.SetActive(true);
}
}
}
}
}
i made a billboard to rotate the health bar if the camera move or the enemy moved so it gives a better look.
EnemyHealthDisplay.cs
using RPG.Stats;
using UnityEngine;
using UnityEngine.UI;
namespace RPG.Combat
{
public class EnemyHealthDisplay : MonoBehaviour
{
public Slider slider;
public Image fill;
Fighter fighter;
private void Awake()
{
fighter = GameObject.FindWithTag("Player").GetComponent<Fighter>();
}
private void Update()
{
if (fighter.GetTarget != null)
{
Health health = fighter.GetTarget;
slider.maxValue = health.GetBaseHealth;
slider.value = health.GetHealthPoints;
}
}
}
}
The odds are, that HealthBar in the second slide is the prefab from the inspector, not the object under the Goblin. (This is a common error, and it has -=all=- the symptoms. Try dragging the HealthBar from the Goblin heirachy to the Billboard script.
yes this worked as expected thank you.
another problem i’m facing is i’m trying to create spawn enemy, to generate enemy i put in an array every 5 minutes.
but first i tried to create the enemies but when i did the patrol path is not working and i think its the same issue you talked about. i need to take the patrol path from the hierarchy , but how i can do this when i instantiate this enemy ??
and i can’t solve the above problem with enemyHealthBar if i went with this approach.
how can i solve this issue ?
Ok to work around this problem i created
SpawnEnemy.cs
using System.Collections;
using UnityEngine;
namespace RPG.Combat
{
public class SpawnEnemy : MonoBehaviour
{
[SerializeField] GameObject[] spawnEnemy = null;
[SerializeField] float timeToWait = 10f;
[SerializeField] float timeBetweenSpawnEnemy = 1f;
void Start()
{
StartCoroutine(CreateEnemies());
}
IEnumerator CreateEnemies()
{
while(true)
{
yield return new WaitForSeconds(timeToWait);
foreach (GameObject enemy in spawnEnemy)
{
Instantiate(enemy, transform.position, Quaternion.identity);
yield return new WaitForSeconds(timeBetweenSpawnEnemy);
}
yield return null;
}
}
}
}
and i added the enemy in the heirarchy, and delayed the spawnEnemy to wait for 10 sec. and took the enemy for the heirarchy and assigned it to spawnEnemy.
and then added in the heirarchy the defaultPatrolPath and assigned it to the enemy.
in the IEnumerator is there a better way of doing this and does this approach cause performance issues.
This got me the idea of how can I implement my exp display gauge. Thank you.
Mine is like in diablo2. a long bar from 0-100% and resets back to 0% whenever the player levels up. then add the excess exp to the new level bar.
That’s my perferred style of XP bar as well.
Is your experience slider the one on the bottom?
It looks like it’s getting the wrong value for the percentage… if the experience is lower than the experience needed, then the bar should be lower than 100% (and in this case, it’s saying 60% but showing more like 140%…
Make sure that the experience as a percentage is experience / experience needed, not the other way round.
For future code pastes, please paste the text of the script into the message window. It’s much easier for me to read and go through it that way.
The only thing I notice right away is taht you’re dividing by Stat.ExperienceReward instead of Stat.ExperienceNeeded. Of course, you may have designed it that way using the same Stat for what’s needed for the player and what’s rewarded by the enemies, so that may be meaningless.
That being said, The display should be matching the percentage shown, as they’re using the same GetPercentage.
When the game is not playing, is the image fitting properly in the frame at 100%?
- Forum User Guides : How to apply code formatting within your post
thanks, I changed with necessary experience and now it works, now it is the text that continues to rise without stopping
If you’re meaning that the percentage doesn’t drop to zero when levelling up, but instead is at a percentage of the experience needed, this is the way that the current experience/level scheme is designed.
To reset to zero %/points, we’d need to reset experience to zero when levelling, and ExperienceNeeded would be the amount needed for just that level. We’d also need to cache the level and save it with Capture/RestoreState.