- Home /
How do i get the player to shrink according to the health?
I am a noob in unity and i am making a script that will make it so that my player will shrink according to his health. I implemented a health system that would decrease health every 2 seconds and it worked fine. Then I added a system that would make it so that my player's size would be the same as his health/1000 (the character was huge!!!) this worked fine. However, at this point I had already added a flip script that would flip the player based on which way he was moving. I tried to make it so if he was already flipped already it would make it so the size of the player was -health/1000. (this is probably really confusing, reading the code will probably help) This didn't work. Any ideas why? using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerHealth : MonoBehaviour { float health = 100f; public Transform playerTransform; Vector2 playerSize;
// Start is called before the first frame update
void Start()
{
StartCoroutine("healthDecrease");
}
IEnumerator healthDecrease()
{
while (health > 0)
{
yield return new WaitForSeconds(2f);
health = health - 2;
StartCoroutine("playerSizeChange");
}
}
IEnumerator playerSizeChange()
{
playerSize.y = health / 1000f;
playerSize.x = health / 1000f;
if (playerSize.x > 0f)
{
playerSize.x = health / 1000f;
}
else
{
playerSize.x = -health / 1000f;
}
playerTransform.localScale = playerSize;
yield return null;
}
}
Answer by Meishin · Sep 06, 2019 at 10:33 AM
Hi @PoseidonA,
Your code seems a bit confused ^^ ;
In playerSizeChange(), Since :
"playerSize.x = health / 1000f;"
Your health is always >= 0,
the else statement playerSize.x = -health / 1000f; will never be called.
Also note that your "StartCoroutine("playerSizeChange");" could be changed by a normal function call since you don't need to delay PlayerSizeChange
Playing on the scale (and setting it negative) of your player is not a good idea because it messes the physics (especially colliders). The "right" way to change your player direction is by switching its renderer texture (with the same mirrored texture) when your character changes direction (i guess your in 2D, else you just have to apply a Z rotation in 3D).
Thank you so much @$$anonymous$$eishin!!! I cleaned up my code and the new code looks like this: playerSize.x = playerTransform.localScale.x; if (playerSize.x > 0f) { playerSize.x = health / 1000f;
}
else
{
playerSize.x = -health / 1000f;
}
playerTransform.localScale = playerSize;
This seemed to work. However, could you explain what you mean by switching the renderer texture. The code i used for flipping the player was actually not $$anonymous$$e, and was a youtuber named Brackeys character controller. Again, thanks.
Uh ok I checked and in fact everybody does what you did, plus Brackeys has much more experience than I have (I learned most from Brackeys :D).
Just know that changing scale and particularly setting it negative can mess up collisions detections and requires more processing of the physic engine, so change scale of your renderer / mesh but preferably not of your colliders/rigidbody (which is prob what you did if you followed Brackeys ^^)
Thank you for the answer @$$anonymous$$eishin. I visited your youtube channel. $$anonymous$$eep making videos bro. Btw, whats ur discord??????
Your answer
Follow this Question
Related Questions
How to Make a "Kill Platform"? 1 Answer
Health subtracts when hit by bullit 2 Answers
2D 360 degress platformer example needed 0 Answers
2D flash health and damage system 0 Answers
Small game, huge size? 1 Answer