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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by schult777 · Mar 03, 2014 at 08:45 AM · coroutinefire1

stopping a Coroutine while it is running.

I have a script for a laser. The script runs great and there is no problems with it. What I want to happen is that after the player has hit fire1 the laser fires for a few seconds and then stops. However I am not able to figure out how to get it to do that. As of right now it fires until you release fire1. Any ideas on how to do that? Here is the script as it is currently written. I need this for a college class and I have not been able to find any answers on it so far. using UnityEngine; using System.Collections;

 public class LaserDesign : MonoBehaviour 
 {
     LineRenderer line;
     
     void Start () 
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
     
     
     void Update () 
     {
         if(Input.GetButtonDown("Fire1"))
         {
             StopCoroutine("FireLaser");
             StartCoroutine("FireLaser", 2.0F);
         
 
         }
     }
     IEnumerator FireLaser()
     {
         line.enabled = true;
         
         while(Input.GetButton("Fire1"))
         {
             Ray ray = new Ray(transform.position, transform.forward);
             RaycastHit hit;
 
             line.SetPosition(0, ray.origin);
 
             if(Physics.Raycast(ray, out hit, 1000))
             {
                 line.SetPosition(1, hit.point);
                 if(hit.rigidbody)
                 {
                     hit.rigidbody.AddForceAtPosition(transform.forward * 50, hit.point);
     }
 }
             else
                 line.SetPosition(1, ray.GetPoint(1000));
             yield return null;
         }
         line.enabled = false;
     }
 }
 
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
Best Answer

Answer by whydoidoit · Mar 03, 2014 at 08:47 AM

Simply terminate you enumerator after two seconds or when the button is released like this:

 using UnityEngine;
 using System.Collections;
 
 public class LaserDesign : MonoBehaviour 
 {
     LineRenderer line;
     
     void Start () 
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
     
     
     void Update () 
     {
         if(Input.GetButtonDown("Fire1"))
         {
             StopCoroutine("FireLaser");
             StartCoroutine("FireLaser", 2.0F);
         
 
         }
     }
     IEnumerator FireLaser(float lengthOfTime)
     {
         line.enabled = true;
             var t = Time.time;
         
         while(Input.GetButton("Fire1") && Time.time - t < lengthOfTime)
         {
             Ray ray = new Ray(transform.position, transform.forward);
             RaycastHit hit;
 
             line.SetPosition(0, ray.origin);
 
             if(Physics.Raycast(ray, out hit, 1000))
             {
                 line.SetPosition(1, hit.point);
                 if(hit.rigidbody)
                 {
                     hit.rigidbody.AddForceAtPosition(transform.forward * 50, hit.point);
     }
 }
             else
                 line.SetPosition(1, ray.GetPoint(1000));
             yield return null;
         }
         line.enabled = false;
     }
 }
 
Comment
Add comment · Show 1 · 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 schult777 · Mar 07, 2014 at 06:40 AM 0
Share

Thank you! It now works just the way I want it to!

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

21 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

Related Questions

Issue With Simultaneous ReadPixels Calls 0 Answers

How to Substitute Coroutine in non-MonoBehaviour Class? 1 Answer

Help needed with WaitForSeconds() 2 Answers

Coroutine... should I worry about synchronization or not? 1 Answer

Segment within a nested If does not get evaluated 2 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