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 akashisayaki · Jan 31, 2021 at 05:38 PM · unity 2ddamagenewbietime.deltatimecharge

Using the increasing and decreasing value function to deal damage

Hi, I'm new to unity c# scripting here, I'm currently making a catapult game and wonder if it's there any idea on how to transfer the increasing and decreasing damage value on every second to deal damage? I want my character to deal max damage after charging up the damage by holding the left mouse button for 2 seconds and if I let go the button, the damage will decrease to 5 under 2 seconds. Here is my code:,Hi, I'm pretty new to unity c# scripting here. I'm currently making a catapult game and wonder if it's there any idea on how to transfer the increasing and decreasing damage value on every second to public int attackDamage? I want my player to deal max damage after holding left mouse button for 2 seconds and then if I let go the mouse button, the damage value will decrease under 2 seconds. This is my code:

public class PlayerDamage : MonoBehaviour {

 public float damage = 5;
 public float valueToIncreaseEverySecond = 1;
 public float maxDamage = 90;
 public float cooldownTime = 2f;
 private float nextChargingTime;

 public int attackDamage = 5;
 public int playerHealth = 500;

 private void Update()
 {
     if (Input.GetMouseButton(0))
     {
         if (damage < maxDamage)
         {
             damage += valueToIncreaseEverySecond * Time.deltaTime;
         }
         else
         {
             damage = maxDamage;
         }
     }

     else
     {
         if (damage > 5)
         {
             damage -= valueToIncreaseEverySecond * Time.deltaTime;
         }
         else
         {
             damage = 5;
         }
     }

     if (Time.time > nextChargingTime)
     {
         if (Input.GetMouseButtonUp(0))
         {
             print("ability cooldown started");
             nextChargingTime = Time.time + cooldownTime;
         }
     }
 }

 public void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.collider.gameObject.CompareTag("Enemies"))
     {
         collision.collider.gameObject.GetComponent<Enemy>().TakeDamage(attackDamage);
     }
     
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by RealFolk · Jan 31, 2021 at 07:38 PM

  public int attackDamage = 5;
     private int maxDamage = 90;
     public int playerHealth = 500;
     public int initialAttackDamage;
     public float countdownTimer = 2f;
     private float initialCountdownTimer;
     public bool isFiring;
 
     private void Start()
     {
         initialCountdownTimer = countdownTimer;
         initialAttackDamage = attackDamage;
     }
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
             attackDamage = initialAttackDamage;
         if (Input.GetMouseButton(0))
         {
 
             if (countdownTimer > 0)
                 countdownTimer -= Time.deltaTime;
             if (countdownTimer < 0)
                 countdownTimer = 0;
             isFiring = true;
         }
         if (Input.GetMouseButtonUp(0) && isFiring)
         {
             attackDamage += Mathf.RoundToInt((1f - countdownTimer / initialCountdownTimer) * maxDamage);
             countdownTimer = initialCountdownTimer;
             isFiring = false;
         }
     }
     public void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.collider.gameObject.CompareTag("Enemies"))
         {
             collision.collider.gameObject.GetComponent<Enemy>().TakeDamage(attackDamage);
         }
 
     }
 
 }
Comment
Add comment · 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
0

Answer by akashisayaki · Feb 01, 2021 at 12:48 PM

Hello, thanks for the reply. I've tried your script, but is it possible to set the max damage and for some reason the enemies doesn't take damage at all.

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 RealFolk · Feb 01, 2021 at 02:00 PM 0
Share

@akashisayaki I added maxDamage into the code above. The script you have is detecting collision with enemies. Your projectile your firing is what needs a script to detect collision. Then you get the attackDamage variable from PlayerDamage script and use it to calculate damage on the Enemy script.

avatar image akashisayaki RealFolk · Feb 01, 2021 at 03:39 PM 0
Share

You're a life saver! The script works perfectly!! Thank you so much

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

120 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

Related Questions

Health below zero but shows negative numbers 1 Answer

Unity Android Screen Glitch 1 Answer

Damage System Doesnt Work? 2 Answers

Assigning different Damage to different bullets,Assigning different damage for each bullet 1 Answer

Newbie Question Regarding Variables 2 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