Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jamesb1 · Jan 03, 2014 at 11:37 PM · ienumeratorslowdown

Applying a temporal weakness on player

First of all, excuse the poor choice for the question title but I really have no idea what else I could call it.

I want to achieve the following scenario:

When the player collides with enemy X0, the player is slowed down for n seconds. If within those n seconds, a player collides with enemy X1 then the timer resets and the player is slowed down further for another n seconds. The above will be repeated until the player is able to avoid from being afflicted by any further enemies of type X.

Now what I have so far is that when my player collides with X; it is slowed down and furthermore if it collides again within those 3 seconds it will slow down further. I also have that if the player is not afflicted for n seconds then the player has normal speed again.

What I DON'T have is a correct time reset. The time is not being reset back to 0 whenever a player collides with more enemies within those n seconds.

Here is my code

 public float slowDown = 0.2f;
     public float weaken = 1f;
     //public float counter = 0f; 
     public float afflictionTime = 3f;
     private static GameObject redCell;
     private Move MoveScript;
     
     // Use this for initialization
     void Start () {
         //set micro affliction (fyi it's very damaging)
         //counter = 0;
         
         if (GameObject.Find ("redCell") == true) {
             redCell = GameObject.Find ("redCell");
             MoveScript = redCell.GetComponent<Move> ();
         }
     }
     
     // Update is called once per frame
     void Update () {
         if (GameObject.Find ("redCell") == true) {
             //Debug.Log (MoveScript.getCurrentAfflictionTime());
             if (MoveScript.isAfflicted ()) {
                 if (MoveScript.getCurrentAfflictionTime() < afflictionTime) {
                     //Debug.Log (counter);
                     Debug.Log (MoveScript.getCurrentAfflictionTime());
                     MoveScript.updateCurrentAfflictionTime();
                     //counter += 1f * Time.deltaTime;
              //   } else if (MoveScript.getCurrentAfflictionTime() >= afflictionTime) {                
             //        resetCell ();
             //        Debug.Log ("heal!");
                 }
             }
         }
     }
     
     void OnTriggerEnter (Collider c) {
         if (c.gameObject.tag == "Player") {
             //counter = 0;
             MoveScript.resetCurrentAfflictionTime();
             //afflictCell ();
             //slowCell ();
             StartCoroutine(affliction(afflictionTime));
         }
     }
 
     IEnumerator affliction(float afflictionTime){
         afflictCell();
         slowCell ();
         yield return new WaitForSeconds(afflictionTime);
         resetCell();
     }
     
     void afflictCell () {
         MoveScript.setAffliction (true);            
         MoveScript.incAfflictionCounter ();
     }
     
     void slowCell () {
         MoveScript.setMoveSpeed (slowDown / MoveScript.getAfflictionCounter ());        
     }
 
     void resetCell () {
         MoveScript.setAffliction (false);
         MoveScript.setMoveSpeed (1f);
         MoveScript.resetAfflictionCounter ();
     }


I have tried fiddling with the IEnumerator method to no avail, I just cannot seem to fix this. Any help would be greatly appreciated.

Thank you :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 04, 2014 at 02:40 AM

Hi,

I didn't understand If I get even weaker on the second hit or if the level of weakness stays the same and only the timer gets reset.

I will show you both to be sure :)

I will also ignore your variables names - would be too easy otherwise :D

Option 1: Time gets reset, level of weakness stays the same

 float lastHitTime = -9999f;
 float weaknessDuration = 3f;
 bool isWeak = false;
 
 IEnumerator weaknessChecker(){
   while(true)
   {
     isWeak = lastHitTime+weaknessDuration > Time.time;
     yield return null; // see ya next frame
   }
 }
 
 void Start()
 {
   StartCoroutine(weaknessChecker());
 }
 
 // if player gets hit, just do this:
 lastHitTime = Time.time;



Option 2: Time gets reset, level of weakness increases on hits

 float lastHitTime = -9999f;
 float weaknessDuration = 3f;
 int weaknessLevel = 0; // 0 means not weak
 
 IEnumerator weaknessChecker(){
   while(true)
   {
     bool isWeak = lastHitTime+weaknessDuration > Time.time;
     if (!isWeak) weaknessLevel = 0;
     yield return null; // see ya next frame
   }
 }
 
 void Start()
 {
   StartCoroutine(weaknessChecker());
 }
 
 // if player gets hit, just do this:
 lastHitTime = Time.time;
 weaknessLevel++;
Comment
Add comment · Show 4 · Share
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 jamesb1 · Jan 04, 2014 at 04:39 PM 0
Share
 bool isWeak = lastHitTime+weaknessDuration > Time.time;

I can't understand how the above makes sense though. How will this output true?

avatar image HappyMoo · Jan 04, 2014 at 04:49 PM 0
Share

the operator "

avatar image jamesb1 · Jan 04, 2014 at 04:52 PM 0
Share

I understood that but I was wondering what it's purpose was and now I understood because I didn't notice that you were setting lastHitTime = Time.time and thought that lastHitTime = -9999f throughout the whole thing!

Thank you!

avatar image HappyMoo · Jan 04, 2014 at 05:04 PM 0
Share

Please accept and upvote the answer

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

19 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

Related Questions

Enumerator, why don't you love me? 1 Answer

Does coroutines are affected by Time.timeScale? 2 Answers

WaitForSeconds before reload not working? 1 Answer

How to make a 3d text writeOut method? 1 Answer

StartCorutine not Working 3 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