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
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;
}
}
}
Geriko
July 26, 2017, 2:06pm
2
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;
}
}