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 DRRosen3 · Nov 15, 2014 at 03:27 AM · coroutinecoroutinesienumerator

Coroutine Not Working

No idea why this isn't working. The IEnumerator MoveGrenade is to move the grenade above the enemy once it collides with the enemy...which it does. The IEnumerator Capture is supposed to create a line renderer from the grenade to the enemy in increments (basically a line extending from the grenade to the enemy slowly). The problem is after the grenade moves above the enemy, it doesn't create the line. Any suggestions?

 using UnityEngine;
 using System.Collections;
 
 public class Grenade : MonoBehaviour {
 
     public float throwTime;
     public float lineDrawSpeed = 6f;
 
     private LineRenderer line;
     private Rigidbody grenadeRigidbody;
     private GameObject grenade;
     private float counter;
     private float distance;
 
     void Awake() {
         line = gameObject.GetComponentInChildren<LineRenderer>();
         grenadeRigidbody = GetComponent<Rigidbody>();
         grenade = gameObject;
     }
 
     void OnCollisionEnter(Collision col){
         if(col.gameObject.tag == "Enemy"){
             StartCoroutine(Capture(col.gameObject));
         }
     }
 
     private IEnumerator MoveGrenade(GameObject col){
         Vector3 moveTo = new Vector3(grenade.transform.position.x, col.transform.position.y+0.5f, grenade.transform.position.z);
         while(Vector3.Distance(grenade.transform.position, moveTo) > 0f){
             grenadeRigidbody.velocity = Vector3.zero;
             grenadeRigidbody.angularVelocity = Vector3.zero;
             grenadeRigidbody.Sleep();
             grenade.transform.LookAt(col.transform.position);
             grenade.transform.position = Vector3.Lerp(grenade.transform.position, moveTo, Time.deltaTime);
             yield return null;
         }
     }
 
     private IEnumerator Capture(GameObject col){
         yield return StartCoroutine(MoveGrenade(col.gameObject));
         Vector3 pointA = gameObject.transform.position;
         Vector3 pointB = col.transform.position;
         distance = Vector3.Distance(pointA, pointB);
         while(counter < distance){
                 counter += .1f / lineDrawSpeed;
 
                 float x = Mathf.Lerp(0, distance, counter);
 
                 Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
 
                 line.SetPosition(1, pointAlongLine);
             }
             yield return null;
     }
 
 }
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 DRRosen3 · Nov 15, 2014 at 04:09 AM 0
Share

UPDATE - I added "Debug.Log("First coroutine has ended");" after line 35...and it never shows up in the console...so apparently the IEnumerator $$anonymous$$oveGrenade is not actually completing.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 15, 2014 at 03:54 AM

You probably want the "yield return null;" in line 53 inside the while loop, don't you? Also you would probably want to use Time.deltaTime when increasing your "counter". Your lineDrawSpeed actually is not a "speed" since if you increase the value it will be slower. I think you want

 counter += lineDrawSpeed * Time.deltaTime;

So the value of lineDrawSpeed is actually in units per second.

Last thing is, are you actually setting the position 0 somewhere? You always just move the second point (index "1") of the line renderer.

Comment
Add comment · Show 3 · 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 DRRosen3 · Nov 15, 2014 at 04:03 AM 0
Share

I moved the "yield return null;" into the while loop... I changed "counter += .1f / lineDrawSpeed;" to "counter += .1f / lineDrawSpeed * Time.deltaTime;"... I added "line.SetPosition(0, pointA);" above line 51...

...but it's still not actually rendering the line. :(

UPDATE - I added "Debug.Log("First coroutine has ended");" after line 35...and it never shows up in the console...so apparently the IEnumerator $$anonymous$$oveGrenade is not actually completing.

avatar image Bunny83 · Nov 15, 2014 at 05:11 AM 0
Share

Yes, sure ^^ You use the "lerp" function again as smoothing function. Depending on how fast your game runs (so how small deltaTime is) you might never reach your target as you always just move a fraction of the remaining distance towards the target. At some point the amount you want to move might be so small that it's actually rounded down to 0. So the last tiny bit you will never move so the distance will never by exactly 0. It's in general not a good idea to expect exact values when you deal with floats.

So either change the condition to ( > 0.01f) or use a different way to move your grenade. Do you actually want a smoothed decelerated movement?

avatar image DRRosen3 · Nov 15, 2014 at 02:01 PM 0
Share

Yes. At first I was using transform.position.translate, but it just makes the grenade suddenly appear in a different position in 3D space. So I'd like to to actually float/hover up to the new position.

Changing the condition to (> 0.01f) worked.

avatar image
0

Answer by ScottYann · Dec 23, 2014 at 05:51 PM

I had this problem also. I had an ienumerator that would not render any lines below a yield WaitForSeconds. I was stumped to and had my brother look at it and he could not find anything wrong with my code.

I executed it with the function reference not the string. I didn't try executing it with a string.

This seems like a problem an engineer should be involved with.

The only solution I had was to run the coroutine on another GameObject. Its ugly but it worked.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Coroutines IEnumerator not working as expected 2 Answers

Use Coroutine when object rotates 2 Answers

How do Coroutines behave with Triggers? 1 Answer

StopCoroutine with IEnumerator not working 2 Answers

coroutine in update method 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