Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PoseidonA · Sep 06, 2019 at 09:37 AM · sizeplatformerhealth

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;
     
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PoseidonA · Sep 07, 2019 at 03:39 AM 0
Share

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.

avatar image Meishin · Sep 08, 2019 at 10:16 AM 0
Share

@PoseidonA,

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 ^^)

avatar image PoseidonA Meishin · Sep 09, 2019 at 12:44 AM 0
Share

Thank you for the answer @$$anonymous$$eishin. I visited your youtube channel. $$anonymous$$eep making videos bro. Btw, whats ur discord??????

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges