I have a player script attached to P1(gameobject). I have a sprite mask as a child in P1health(gameobject).
in my player script attached to P1, I have current health and max health so i am creating a percentage upon each hit. The problem is I am trying to move the sprite mask when my percentage is changing. I have searched everywhere but I am not able to solve this. For example:
Initial position(x value) of sprite mask is 9.1 (full health) final position(x value) of sprite mask is 1.4 (zero health)
I am trying to move the sprite bar as the percentage is going down between these 2 values mentioned above.
So you have two arbitratry values and you need to get values between them. You probably didn’t search everywhere as there are many tutorials on the topic of health bars.
Mathf.Lerp could be used here to do this simply by interpolating between two values based on a third. The third value needs to be between 0-1 so you need to normalize your value to that range. It’s very easy if your health is something like 0-100 or 0-1000, just divide it by the maximum value. Then your values are in the range.
@Olmi Thank you very much for you reply. I understand your point however I believe I wasnt clear on my side. I have searched for health bars and I have used it on enemy as a slider which was very easy to follow. Now I am making a health bar for player which should not be moving along with player it self therefore I am a new game object instead of making it child. Not only that Iam not using a slider for this health bar, I got a custom health bar from asset store png files where the back(empty bar) and front(full bar) is there. I put it in a canvas to anchor it at a location. Then i am using a sprite mask where the front bar is disappearing as the mask is moved horizontally. So what I am trying to do is, I mad a new script in healthbar object where I am calling the return function from player script:
player p1 = gameObject.GetComponent<player>();
health = p1.GetHealth();
maxhealth = p1.GetMaxHealth();
Debug.Log(health);
Debug.Log(maxhealth);
Where the player script is having these values:
[SerializeField] float maxhealth = 200;
[SerializeField] float health = 200;
public int GetHealth()
{
return health;
}
public float GetMaxHealth()
{
return maxhealth;
}
Now healthbar script is returning no object reference value but it return in player script. I am not sure why. Again I am sorry I just started learning these courses. But I assure you i am trying to search as much as possible before asking a question here. Thanks again.
EDIT: also I am trying to use lerp as you mentioned which is really helpful and what i needed exactly.
@Olmi Understood. Thank you very much for your help. Now i understand your point. However i have read everywhere that find method is not recommended to use as its very slow. Also one more question.
I am trying to access the child (New Sprite Mask) in plalyer script however no luck. I am using the following
Yes definitely don’t use find in Update and things that happen often if possible but I just gave it as an example. It’s fine to do it when you initialize things in Start (for example), just cache it.
Is there any specific reason why you can’t manually assign all the required components in Inspector UI? Would be just easier to drag and drop them in. And also more flexible when you want to change/alter assets etc.
If you point directly to a child index, it might not even exist and you get out of bounds errors.
You should check if the object has children before trying to get them, you can check by comparing against Transform.childCount.
if (someGameObject.transform.childCount > child_ID) {
// Do something
}
@Olmi On the point explanation. I will try keep that in mind.
The reason I am not making it in the gameobject is because if I try to move the player, the UI (healthbar) moves along with it. So thats why I am made it in another gameobject. As far as dragging that gameobject to playerobject, I am not sure how to code that. For example what I want is to drag the child layer to P1 object having the script, but it does not allows me even I am making it public. Thats why I was asking what is the simplest way to do it even its dragging the Ui object. I want to move the masking script as shown in the image by the P1 script. Thats why I am trying to use this script but it is not allowing me to even before dragging the object: