#Healthbar logic
1 messages · Page 1 of 1 (latest)
current health will be whatever maximum health is
executes the code every frame?
yeah
so.. if you're setting the currentHealth to maxHealth every frame
say maxHealth is 10. you're setting currentHealth to 10
in the line after that, you do
healthBarSlider.fillAmount = currentHealth / maxHealth;
so that would mean 10/10
equals to 1
well the tutorials are crap then
so your healthBar will always look full
lol
i dont know i managed to get the script working at one point
you understand the issue with that?
i feel like my game has a bug and its not going to work
i dont care that you got it working at one point
do you understand what went wrong there?
i understand the issue
i had trouble enough getting the enemy to move via nav mesh
you want the health to reduce when the projectile hits the wall, right?
upon entering into the collision between two objects?
pretty much, yeah
thing is, those objects need to be set up a specific way for it to even be called
so please put a Debug.Log inside and outside of the if statement. The one that's inside the OnCollisionEnter
please show me your code snippet once you've done so
i mean i did do a debug log and the message came up in the console window but the health bar wouldnt decrease
we already covered why your health bar doesn't decrease
please dont jump about
confirm that your if statement actually runs
the one inside OnCollisionEnter
look btw im sorry im new, but i really am trying to understand code as I use it
and okay
do you know how to confirm that?
hang on can you please just tell me how it would look in code? im sorry to ask.
the basics for me im still learning
public void OnCollisionEnter(Collision collision)
{
Debug.Log("This will be printed if OnCollisionEnter is called");
if (collision.gameObject.CompareTag("projectile"))
{
Debug.Log("This will be printed if the object that this script is on collided with another object that is tagged as projectile");
healthBarSlider.fillAmount -= 10;
Destroy(gameObject, 3);
}
}
thank you
i'll assume you understand that
hang on i just need to read it a few times so i can understand it
need some kind of message in it dont i?
it comes up with error anyway
I already wrote the message for you
show me what you have right now,
and what the error is
oh ok i thought you were explaining it, i realisw now you didnt use // but ''
so now confirm that the log inside the if statement actually prints
nothing appears in console window
do you not wonder why I could write it in this format?
so you're saying none of those logs printed
i dont know how you get yours up on here like a mini screen but mine is a link
not in console window no
okay
so that means your objects aren't set up correctly
they both need colliders that are not marked as isTrigger
and at least one of the objects involved in the collision must have a rigidbody that is not kinematic
neither the tower collider or enemy projectile collider have 'istriggered' ticked
in the future if you run into issues where your collision functions aren't being called, you can use this link to trouble shoot it
https://help.vertx.xyz/programming/physics-messages
It's pinned in #💻┃code-beginner as "Debugging Physics Messages"
the tower target has no rigidbody at all
the projectile has a rigidbody but is not kinematic
how about you just show me the inspector for both those objects
and which object is this script attached to?
its attached to the UI image, which is a child of the tower
OnCollisionEnter works between the object that the script is attached to.. and the other object that it's colliding with
your UI Image isn't involved in the collision, why is it having the OnCollisionEnter
because i want the fill amount of the image to change upon collision
yes but is the UI itself colliding with objects?
i have attached various collision scripts to the main game objects in the past all which come back with 'null reference' errors
one thing to note is that if the object hierarchy has a rigidbody, the OnCollisionEnter will be called for the object that has the rigidbody
did you really follow a tutorial for this
are you attempting to modify the tutorial code?
or are you following it strictly?
i dunno im about ready to give up with it all. nothing is simple. I mean to not be able to have a health bar decrease through collision is so stupid .
or not easily through collision anyway
if i put a rigidbody on the tower target, both the tower and the UI healthbar are forced back when the enemy projectile collides
rigidbody should be on the object that's moving
so it really should be on the projectile
and what do you mean you dunno. that's a yes or no question
you're new. nothing is simple or easy when you're new
no, i trust that you have it set up correctly
but this
dont put the rigidbody on the tower. put it on the projectile. Rigidbody should be on the object that's moving
yea it is already on there
and the OnCollisionEnter code should be on a script that's attached to the tower
so projectile will hit tower and hit it like its a collision
not the UI
i have a tower and tower target (empty game object with box collider)
which one should it go on?
i assumed tower target
the one with the collider
OnCollisionEnter is triggered when a collision happens between two colliders
I have to go, but i'll give you a rough idea on how you should be going about this
the OnCollisionEnter should be reducing the currentHealth, not the maxHealth
and you'll need to get a reference to the health bar UI in the script that has the health in order to modify it
A few basic ways to get references in Unity.
Directly-
GetComponent 0:38
/GetComponentInChildren
/GetComponentInParent
Public Variable- 3:00
Find- 5:01
Find 5:39 (by name)
FindWithTag 6:14 (by tag)
FindObjectOfType 6:51 (by component)
Interaction- 7:15
OnCollisionEnter
/OnTriggerEnter
this video might come in handy for what you're doing
not health bar specific, but getting references and physics collision stuff
ok cheers, i might blow my brains out but if not hopefully this will help what youve provided