Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TwitchyOrphan · Nov 10, 2021 at 03:12 PM · damage

Console showing damage indicator, but damage multiplication is incorrect.

Hello,

I need my enemy to do 1 damage, trap to do 2 damage, and repair kit to heal 5 damage. My repair kit works fine and heals only when player is below max health and for the correct amount. I copied the same script for my trap and altered a bit to damage the player and when the trigger is activated, player goes from 10 health to 0 instantly.

Here is my trap script:

 private void OnTriggerEnter(Collider other)
 {
     PlayerController controller = other.GetComponent<PlayerController>();

     if (controller != null)
     {
         if (controller.currentHealth > 0)
         {
             controller.ChangeHealth(-2);
             Destroy(gameObject);
             Debug.Log("Player takes 2 damage from damage zone.");
         }
     }
 }

And here is the relevant information from my player controller script:

 public int maxHealth = 10;
 public int currentHealth;

 public float timeInvincible;
 public float invincibleTimer;
 public bool isInvincible = false;    

 void Start()
 {
     playerRb = GetComponent<Rigidbody>();
     currentHealth = maxHealth;
 }

 void Update()
 {
     PlayerMovement();
     LayTrap();

     if (isInvincible)
     {
         invincibleTimer -= Time.deltaTime;
         
         if (invincibleTimer < 0)
         {
             isInvincible = false;
         }
     }
 }

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Enemy"))
     {
         collision.gameObject.GetComponent<EnemyController>().ChangeHealth(-1);
         Debug.Log("Enemy takes 1 damage from player."); 
     }
 }

 public void ChangeHealth(int amount)
 {
     currentHealth = Mathf.Clamp(currentHealth = amount, 0, maxHealth);

     if (amount < 0)
     {
         if (isInvincible)
         {
             return;
         }

         isInvincible = true;
         invincibleTimer = timeInvincible;
     }

     if (currentHealth < 1)
     {
         Debug.Log("Game Over!");
     }

     if (currentHealth > 10)
     {
         currentHealth = 10;
     }
 }
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 evskii · Nov 10, 2021 at 07:37 PM

The clamp should say

 currentHealth = Mathf.Clamp(currentHealth += amount, 0, maxHealth);

because what is happening is you are saying currentHealth = amount, and that is just setting the health to be equals to the damage you are taking rather than taking the damage away, then because your clamp is saying don't go below 0, it is just setting the health to be 0.

Comment
Add comment · Show 2 · 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 TwitchyOrphan · Nov 10, 2021 at 07:47 PM 1
Share

Thank you very much! Oversight when copying code, but I doubt I would've known if I'd seen it. I appreciate the explanation as well. Makes sense now and I've corrected the same issue for my enemies now :D

avatar image evskii TwitchyOrphan · Nov 10, 2021 at 07:48 PM 1
Share

No worries! I make these mistakes many times a day.

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

132 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 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

Having problems with damage script 1 Answer

Problem with weapon and projectiles system in dealing damage to enemies? 1 Answer

Child object collision 2 Answers

Collision Damage, Hit points and Instantiate 1 Answer

Robot Not Taking Damage 0 Answers


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