Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by MagJ99 · Jan 04, 2017 at 01:05 PM · 2d-platformertime.time

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);
             }
 
 
         }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MagJ99 · Jan 04, 2017 at 01:11 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges