#Healthbar logic

1 messages · Page 1 of 1 (latest)

halcyon pilot
sage cedar
#

current health will be whatever maximum health is

halcyon pilot
#

okay

#

do you know what Update does?

sage cedar
#

executes the code every frame?

halcyon pilot
#

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

sage cedar
#

well the tutorials are crap then

halcyon pilot
#

so your healthBar will always look full

sage cedar
#

lol

halcyon pilot
#

no

#

more likely you followed it wrong

sage cedar
#

i dont know i managed to get the script working at one point

halcyon pilot
#

you understand the issue with that?

sage cedar
#

i feel like my game has a bug and its not going to work

halcyon pilot
#

i dont care that you got it working at one point

#

do you understand what went wrong there?

sage cedar
#

i understand the issue

halcyon pilot
#

okay

#

moving on

#

that's not the only issue

sage cedar
#

i had trouble enough getting the enemy to move via nav mesh

halcyon pilot
#

you want the health to reduce when the projectile hits the wall, right?

sage cedar
#

cant believe there wasnt an easier way

#

yes

halcyon pilot
#

do you understand what OnCollisionEnter does

#

how it gets called

sage cedar
#

upon entering into the collision between two objects?

halcyon pilot
#

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

sage cedar
#

i mean i did do a debug log and the message came up in the console window but the health bar wouldnt decrease

halcyon pilot
#

we already covered why your health bar doesn't decrease

sage cedar
#

so i have done various things like that

#

okay

halcyon pilot
#

please dont jump about

#

confirm that your if statement actually runs

#

the one inside OnCollisionEnter

sage cedar
#

look btw im sorry im new, but i really am trying to understand code as I use it

#

and okay

halcyon pilot
#

do you know how to confirm that?

sage cedar
#

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

halcyon pilot
#
    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);

        }
    }
sage cedar
#

thank you

halcyon pilot
#

i'll assume you understand that

sage cedar
#

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

halcyon pilot
#

I already wrote the message for you

#

show me what you have right now,

#

and what the error is

sage cedar
#

oh ok i thought you were explaining it, i realisw now you didnt use // but ''

halcyon pilot
#

so now confirm that the log inside the if statement actually prints

sage cedar
#

nothing appears in console window

halcyon pilot
#

Show me your snippet please

#

your code

sage cedar
#

can i just paste it in here or not?

#

if not ill use link you provided

halcyon pilot
#

to see how you can post code in this server?

halcyon pilot
sage cedar
halcyon pilot
#

so you're saying none of those logs printed

sage cedar
#

i dont know how you get yours up on here like a mini screen but mine is a link

#

not in console window no

halcyon pilot
sage cedar
#

okay

halcyon pilot
#

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

sage cedar
#

neither the tower collider or enemy projectile collider have 'istriggered' ticked

halcyon pilot
sage cedar
#

the tower target has no rigidbody at all

#

the projectile has a rigidbody but is not kinematic

halcyon pilot
#

how about you just show me the inspector for both those objects

#

and which object is this script attached to?

sage cedar
#

its attached to the UI image, which is a child of the tower

halcyon pilot
#

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

sage cedar
#

because i want the fill amount of the image to change upon collision

halcyon pilot
#

yes but is the UI itself colliding with objects?

sage cedar
#

i have attached various collision scripts to the main game objects in the past all which come back with 'null reference' errors

halcyon pilot
#

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?

sage cedar
#

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

halcyon pilot
#

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

sage cedar
#

yes i have for the majority

#

do you still want screen shots of inspector

#

?

halcyon pilot
#

no, i trust that you have it set up correctly

halcyon pilot
sage cedar
#

well i dont know that i do

#

you just said the script shouldnt be on UI Image

halcyon pilot
#

dont put the rigidbody on the tower. put it on the projectile. Rigidbody should be on the object that's moving

sage cedar
#

yea it is already on there

halcyon pilot
#

and the OnCollisionEnter code should be on a script that's attached to the tower

sage cedar
#

so projectile will hit tower and hit it like its a collision

halcyon pilot
#

not the UI

sage cedar
#

i have a tower and tower target (empty game object with box collider)

#

which one should it go on?

#

i assumed tower target

halcyon pilot
#

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

#

this video might come in handy for what you're doing

#

not health bar specific, but getting references and physics collision stuff

sage cedar
#

ok cheers, i might blow my brains out but if not hopefully this will help what youve provided