Time.FixedTime does not update after each frame
Hello everyone
Well, I am currently working on a 2D Plattformer game. It's just a simple project for school.
I want that the enemy has something like a little "Brain". That means, if it spots the player, it will follow him for a certain time (like 10 seconds). So, I save every time, while the enemy can still spot the player, in a variable. Once he cannot see him anymore, the time shall count 10 seconds and he goes into idle mode again. But, if he finds him again, all start again.
However, this does not really work. I can just save the time at the beginning of the game in the variable. Afterwards, the time does not update each frame. Is there a way to do this quite smoothly?
You need to know that I am beginner and, if you haven't noticed yet, my English is pretty bad. I really apologise.
My code is here: void FixedUpdate () {
// 2 linecasts for spotting and attacking the player
Vector2 lineCastPosPlayerSpot = traEnemy.position.toVector2() + traEnemy.right.toVector2() * myWidth;
Vector2 lineCastPosPlayerAttack = traEnemy.position.toVector2() + traEnemy.right.toVector2() * myWidth;
Debug.DrawLine(lineCastPosPlayerSpot, lineCastPosPlayerSpot + traEnemy.right.toVector2() * 10.0f, Color.yellow);
booPlayerSpotted = Physics2D.Linecast(lineCastPosPlayerSpot, lineCastPosPlayerSpot + traEnemy.right.toVector2() * 10.0f, playerSpot);
Debug.DrawLine(lineCastPosPlayerAttack, lineCastPosPlayerAttack + traEnemy.right.toVector2() * 1.0f, Color.red);
booEnemyAttack = Physics2D.Linecast(lineCastPosPlayerAttack, lineCastPosPlayerAttack + traEnemy.right.toVector2() * 1.0f, playerSpot);
//that is a method, which detects where the player and the enemy is and in which direction the enemy is watching
funSetting();
//idle mode, Enemy is just walking around
if (!booPlayerSpotted && !spotReminder)
{
speed = 1;
ve2EnemyPace = ri2EnemyBody.velocity;
ve2EnemyPace.x = traEnemy.right.x * speed;
ri2EnemyBody.velocity = ve2EnemyPace;
anim.SetBool("EnemyHeavy_Run", true);
if (!booEnemyIsGrounded || booEnemyIsBlocked)
{
ve2CurrRot = traEnemy.eulerAngles;
ve2CurrRot.y += 180;
traEnemy.eulerAngles = ve2CurrRot;
}
}
//Player gets spotted by the LineCast
else if (booPlayerSpotted)
{
spotReminder = true;
//time gets saved after the player was spotted
fltLastSpottedPlayer = Time.fixedTime;
Debug.Log("LastSpottedTime : " + fltLastSpottedPlayer);
Debug.Log("PlayerSpotted bool : " + booPlayerSpotted);
}
//that is the part, where my Enemy chases the player
if (spotReminder && !booEnemyAttack)
{
speed = 6;
// Debug.Log("Player" + xPlayer);
// Debug.Log("Enemy" + traEnemy.position);
if (booPlayerLeftFromEnemy && booEnemyMovesLeft)
{
funPlayerInReachTest();
funEnemyWatchesLeft();
} else if (!booPlayerLeftFromEnemy && !booEnemyMovesLeft)
{
funPlayerInReachTest();
funEnemyWatchesRight();
} else if (booPlayerLeftFromEnemy && !booEnemyMovesLeft)
{
funPlayerInReachTest();
funEnemyWatchesLeft();
} else if (!booPlayerLeftFromEnemy && booEnemyMovesLeft)
{
funPlayerInReachTest();
funEnemyWatchesRight();
}
//after each frame should the new Time be saved in the variable
fltTimeSinceLastSpotted = Time.fixedTime;
if (fltTimeSinceLastSpotted - fltLastSpottedPlayer > 6.0f)
{
spotReminder = false;
}
}
// the rest is more or less uninteresting. Having said that I assume that I have the same problem there with my Hit delay...
else if (booEnemyAttack)
{
ve2EnemyPace = ri2EnemyBody.velocity;
ve2EnemyPace.x = 0;
ri2EnemyBody.velocity = ve2EnemyPace;
anim.SetBool("EnemyHeavy_Chasing", false);
anim.SetBool("EnemyHeavy_Run", false);
if (booAttackBegin)
{
fltAttackBegin = Time.fixedTime;
booAttackBegin = false;
anim.SetBool("Hit", false);
}
float check = Time.fixedTime;
// Debug.Log("Check: " + check);
// Debug.Log("flotAttackBegin: " + fltAttackBegin);
// Debug.Log("Differnz" + (check - fltAttackBegin));
if (check - fltAttackBegin > 2.0f)
{
anim.SetBool("Hit", true);
booAttackBegin = true;
scrPlayerHealth.funPlayerDamage(1);
}
// Debug.Log("booAttackBegin: " + booAttackBegin);
}
}
Ups, I forgot:
I appreciate every help I can get and already wanna say thanks to you in advance :)
In addition, I already tried a "waitForSeconds"-$$anonymous$$ethod with IEnumerate (or something like that), but that did not work. I am pretty sure it only works in the start-Function not in the update-Function.
Your answer

Follow this Question
Related Questions
How to have UI Manager come up on collision? 0 Answers
How To Do Basic 2D Movement? 1 Answer
Parsing error? 1 Answer
Player can only jump once!? 0 Answers
Sprites appears corrupted in standalone build, but not editor. 0 Answers