
It looks like you're new here. If you want to get involved, click one of these buttons!
I watched the Tutorial from brackey on how to create a health bar.
The Video: https://www.youtube.com/watch?v=BLfNP4Sc_iA&list=WL&index=3
But i dont know, how to let the healthbar show less lifes, if the Player sprite touches a thing.
can someone explain me how to do that?
Sorry for my bad english it's not my Native Language. I hope you're able to understand the question.
Oh, and i asked the question in the programming part too. By accident... Sorry... dont know how to delete the question...
ok, in your player script you need a method like this:
public void TakeDamage(float damage) { currentHealth -= damage; healthBar.SetHealth(currentHealth); }
if you already have a method with the same name just call it DamagePlayer(float damage) or something.
you want to check now if the player collides with something so the player and the other object should have a collider(you can add one by just click add component in the inspector and choose one that fits your character). now you have to create a new method(you should put it in a script which is attached to the thing that makes damage):
void DamagePlayerOnCollision () { //detect player in range Collider[] hitPlayer = Physics.OverlapSphere(transform.position, radius, playerLayers); //damage player foreach(Collider player in hitPlayer) { player.GetComponent<Player>().TakeDamage(damage); } }
you should put DamagePlayerOnCollision() in the update method. you should put the following to the top of the script:
public float radius = 5f; //5f or whatever you like public Player player; public LayerMask playerLayers;
i prepared you the code but you should try to understand it anways because if you dont understand it your game makes progress but you dont. so do some research on the topics (colliders and stuff) or ask. Hope i could help you and you manage do put it into your game, if not explain what you did and i try to help you. i hope i could help :)