- Home /
Make enemy wait before attacking player
so I have this script that detects when the zombie touches the player and damages the player, but there's one problem the zombie pretty much insta-kills the player I need a way to delay the damage by 1 second heres my script.
if (Decol.IsTouching (playerCol)) {
player.GetComponent<Player> ().curHealth -= 1;
}
and yes, I have tried a coroutine
Answer by Reynarz · Jul 18, 2017 at 04:43 PM
You can do something like this using Time.deltaTime and sustracting some value to _timerToAttack.
And when the variable get the value of 0, the enemy can attack and later _timerToAttack get the default value.
Remember, put the "_canAttack" variable in your enemy attack method
private float _timerToAttack = 3f; //Random Timer.
private float _time = 1f;
private bool _canAttack = false;
private bool _playerTouched = false;
private private void Update()
{
TimerToAttack();
}
private void TimerToAttack()
{
if(_playerTouched )
{
if (_timerToAttack > 0)
{
_canAttack = false;
_timerToAttack -= _time * Time.deltaTime;
}
else
{
_canAttack = true;
_timerToAttack = 3f;
}
}
}
Answer by Geriko · Jul 26, 2017 at 02:06 PM
make a Time variable WaitingTime or something, and a Time const WaitTimeUntilAttack
if (Decol.IsTouching (playerCol)) {
waitingTime-=Time.deltaTime;
if(waitingTime<0){
player.GetComponent<Player>().curHealth -= 1;
waitingTime=WaitTimeUntilAttack;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to WAIT for RPC to return/finish? 1 Answer
Pause a script completely 3 Answers
Help with waiting / coroutine c# 1 Answer