- Home /
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);
}
}
}
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);
}
}
}
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.
@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.
You're a life saver! The script works perfectly!! Thank you so much
Your answer
![](https://koobas.hobune.stream/wayback/20220613024319im_/https://answers.unity.com/themes/thub/images/avi.jpg)