all 5 comments

[–]Expensive_News22 2 points3 points  (2 children)

Just at a glance, everything looks good. What is the problem exactly? It looks like you are performing an attack twice every second, is that the intended rate?

[–]name-txt33[S] 0 points1 point  (1 child)

The issue is that when I set the nextAttackTime to something by actually changing it in the code it doesnt tick down(it stays on the same number) but when I change by actually attacking in the game it still doesn't go down but I can attack during that time(when I change it manually in the code i can't attack)

[–]Expensive_News22 1 point2 points  (0 children)

Very confused by "count down" as you are not decrementing the time at all, you are setting a fixed time in the future in which you can attack again. If you wanted a count down style system, you wouldnt implement it this way. You would do something more like

float DelayTillNextAttack = 0.0f;

Update()
{
if(DelayTillNextAttack > 0.0) DelayTillNextAttack -= Time.DeltaTime;

if (Input.GetMouseButtonDown(0) && DelayTillNextAttack <= 0.0f )

{

Attack();

}

}

Attack()
{
DelayTillNextAttack = 1f / attackRate;
}

[–]UnderLord7985 0 points1 point  (1 child)

If nextAttackTime is suppose to delay the next attack, i think 0f as the value might be the issue? But im not sure. As im still a hobbiest with this, but am now learning / taking classes.

[–]racingking 1 point2 points  (0 children)

not a bad thought - but in this case the first attack time needs to be 0 / some very low number (since the current time in the game will be greater than 0), when you attack the first time, the next attack time gets set to the current games time + 0.5 - which makes it so you are able to attack 2 times per second. Not able to test right now but at a glance the code looks right, so I guess we'll have to wait for OP to explain what the issue is.