Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Grievemourne · Aug 12, 2015 at 06:41 PM · coroutinewaitforsecondstimescaletime.timescale

What's stopping 'WaitForSeconds' from working?

I have the script with the coroutine:

 float maxDuration = 0.5f;
     public IEnumerator slowDown = null;
     float countdown = 0;
 
 
     public IEnumerator SlowTime(float scale, float dur){
         if(dur > maxDuration){
             dur = maxDuration;
         }
         if(dur < 0){
             dur = 0.001f;
         }
 
         countdown = 0;
         Time.timeScale = scale;
         Debug.LogWarning("Scale: " + Time.timeScale + " - Duration: " + dur);
         yield return new WaitForSeconds(dur * Time.timeScale);
         Time.timeScale = 1;
     }

and the script setting it ( where The final "Slow" function is probably the most relevant.):

     bool meleeDeflectionEnabled;
     bool projectileDeflectionEnabled;
     bool spellDeflectionEnabled;
     GameObject player = null;
     States playerState;
     GameObject enemy = null;
     public Vector3 deflectDirection;
     float distance = 1.5f;
     GameObject instancePrefab;
 
     float timeScale = 0.1f;
     float slowDuration = 0.5f;
     TimeControl timeControl;
 
     void Start(){
         instancePrefab = Resources.Load("DeflectionTarget",typeof(GameObject)) as GameObject;
         player = transform.parent.gameObject;
         //deflectDirection = player.GetComponent<CombatInput>().direction;
         playerState = player.GetComponent<States>();
         timeControl = GameObject.FindGameObjectWithTag("Vital").GetComponent<TimeControl>();
     }
 
     //This part is going to be called immediately, and only once, when something enters. I'm going to have to apply this script through script because of that...
     //since I can't .enabled = false the OnTriggerEnter part, only updates, which aren't in use.
 
     void OnTriggerEnter(Collider attack){
         //if player is deflecting and if collider is attack,
         CheckAttack(attack);
 
     }
 
     void CheckAttack(Collider attack){
         if(attack.tag == "Attack"){
             if( attack.GetComponent<DamageVolume>() != null){
                 AttackTypes attackType = attack.GetComponent<DamageVolume>().attackType;
                 if( attackType == AttackTypes.Melee){
                     enemy = attack.transform.parent.gameObject;
                     Deflection(attack.transform, deflectDirection);
 
                 }
             }
         }
     }
 
     void Deflection(Transform attack, Vector3 deflectDir){
         //What does deflection mean? It means the the opponent is pushed towards a target location, and facing a certain direction. 
         Slow();
         Vector3 deflectPosition = (deflectDirection * distance) + player.transform.position;
 //        Instantiate(instancePrefab, deflectPosition, Quaternion.identity);
 
         //Target location Have them both face it, and move toward it.
         //should I maybe handle the movement in the enemy AI? yeah.
         //where should the AI get the position data from? From the deflection volume? Sure.
 
         //enemy.transform.position
         enemy.GetComponent<EnemyAI>().deflectionPosition = deflectPosition;
         enemy.GetComponent<EnemyAI>().deflectedTime = enemy.GetComponent<EnemyAI>().defaultDeflectTime;
         //The attack is also displayed as nullified for the player.
         //stop damage of all attacks in those frames.
         //Get distance between attacker and target point, and have set the velocity so it happens over a set amount of seconds.
         //Rotate a set amount per second. Decided the the difference in degrees. 
         //Get damage data and reduce the damage. 
         //attack
         playerState.deflecting = false;
 
         Destroy(this);
     }
 
     void Slow(){
         //Check if stopping coroutine is necessary
         if( timeControl.slowDown != null){
             StopCoroutine(timeControl.slowDown);
         }
         //Set corouting data
         timeControl.slowDown = timeControl.SlowTime(timeScale, slowDuration);
         //start  the coroutine
         StartCoroutine(timeControl.slowDown);
         //Set buffers.
         timeScale *= 2;
         slowDuration *= 0.6f;
     }



I'm extremely confused as to what's happening since I get feedback that the script has been entered, but slowdown doesn't always happen in a similar script - but this one in particular only slows down, and doesn't recover, as if the " yield return new WaitForSeconds(dur * Time.timeScale);" line either never starts or never resolves.

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
1

Answer by Grievemourne · Aug 12, 2015 at 06:55 PM

Answering my own question: I thought I got around the code getting killed when the script was destroyed , but I was calling the StartCoroutine in the script that was getting destroyed, (by using StartCoroutine(timeControl.slowDown);) instead of the TimeControl script ( with timeControl.StartCoroutine(timeControl.slowDown);)

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

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

24 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

Related Questions

Is there a way to make WaitForSeconds ignore time.timescale? 1 Answer

(Rapid fire) How to accurately wait for a very short amount of time? 4 Answers

Delay after input? 1 Answer

yield return WaitForSeconds not working 2 Answers

Changing text on Canvas Text is very slow / unresponsive 1 Answer


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