- Home /
How to reset attack animations using time.deltatime
Hi everyone, I am in the process of making two units fight each other in an RTS. I was writing some code and trying to use Time.deltaTime but I noticed that it continuously updates time so it wont trigger my code. I am looking for alternatives that would produce the same effect. I am looking to have the units attack duration to take 1 seconds, and in that 1 second to change its color then reset so I know it has animated. I have provided my code below. Thank you,
public void FixedUpdate(){
if (enemyAttacking == true) {
if(atkCounter<atkDuration){
renderer.material.color = new Color32(255, blueColor, 255, 1);
atkCounter +=Time.deltaTime;
blueColor-=1;
print (atkCounter);
}
if(atkCounter>=atkDuration){
Debug.Log ("-----------------------the enemy hit a player unit!");//simple test for enemy attack
atkCounter=0;
blueColor=255;
}
}
}
Answer by 2dfruity · May 30, 2014 at 12:45 PM
i use a cooldown and Time.time. because if time always increases, and if you add to it when storing into another var you can compare it to Time.time and have an "attack rate"
if (Time.time >= coolDown){
GoulAttack();
coolDown = Time.time + attackRate;
}
Answer by wijesijp · May 30, 2014 at 05:02 AM
I think the best way to do this is to add a trigger to the end of your attack animation. That way you don't have to do much in FixedUpdate.
Set the trigger to call a function like this...
public void AnimationCompleted(int animID)
{
blueColor = 255;
}
public void FixedUpdate()
{
if (enemyAttacking == true)
{
renderer.material.color = new Color32(255, blueColor, 255, 1);
blueColor -= Time.deltaTime;
}
}
@wijesijp I appreciate your comment, but i do not have an attach anim yet. I was using the color change as a placeholder for attack anim. I saw another tutorial use timer.deltatime to control his anims and reset them using what I used above. Is there any way to have this way work? Thanks!
There are few things you can check
1) Since you are in FixedUpdate you must use Time.fixedDeltaTime
2) I think the problem line may be this blueColor-=1;
It should be blueColor -= Time.fixedDeltaTime;
Your answer
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Different game speed 2 Answers
Get actual Time.deltaTime from FixedUpdate() 0 Answers
RTS faction color 0 Answers