Cant post question in Unity answear so im asking it here instead!

Hi! When i press Mousebutton1 i want to call attack one time and then wait like 2 sec until it can be called again (this way spamming mousebutton1 wont be possible) but i cant figure out how to do this. Can you help me? :slight_smile:
The code is in JS

#pragma strict

var TheDamage : int = 25;
private var Distance : float;
var MaxDistance : float = 1.5;
var TheAnimator : Animator;
var DamageDelay : float = 0.6;
var Wait : boolean = false;

private var Hit01Streak = 0;
private var Hit02Streak = 0;

function Update ()
{
if (Input.GetButtonDown(“Fire1”))
{
Wait = true;
AttackDamage();
}
}

function AttackDamage ()
{
if (Random.value >= 0.5 && Hit01Streak <= 2)
{
TheAnimator.SetBool(“Hit01”, true);
Hit01Streak += 1;
Hit02Streak = 0;
}
else
{
if (Hit02Streak <= 2)
{
TheAnimator.SetBool(“Hit02”, true);
Hit01Streak = 0;
Hit02Streak += 1;
}
else
{
TheAnimator.SetBool(“Hit01”, true);
Hit01Streak += 1;
Hit02Streak = 0;
}
}

yield WaitForSeconds(DamageDelay);
//Actual attacking
Wait = false;
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
if (Physics.Raycast (ray, hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage(“ApplyDamageEnemy”, TheDamage, SendMessageOptions.DontRequireReceiver);
Debug.Log(“The Player Has Attacked”);
}
}

TheAnimator.SetBool(“Hit01”, false);
TheAnimator.SetBool(“Hit02”, false);
}

var Wait : float = 0.0;

function Update() {
    Wait -= Time.deltaTime;
    if (Wait > 0.0) return;
    if (Input.GetButtonDown("Fire1")) {
        Wait = 2.0;
        AttackDamage();
    }
}
1 Like

Omg thank you som much! one more question if its ok: it deals damage 3 times in one punch, you know why?
:frowning:

Because either AttackDamage() applies it 3 times or you have 3 of these scripts somewhere.

I cant find the script applied anywhere else

hmm suddenly it works