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