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 /
avatar image
0
Question by TheRockRipper · Jun 15, 2017 at 05:06 PM · raycastinginfinitelaserwhile-loop

I'm a coding noob. For the life of me I'm not able to figure out why the while loop is going infinitely? Any and all help is appreciated. Here is the code.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class LaserScript : MonoBehaviour {

 LineRenderer laser;
 int count;
 //Vector3[] laserPoints = new Vector3[10];

 void Start ()
 {
     laser = GetComponent<LineRenderer> ();
     laser.enabled = false;
     //laserPoints [2] = new Vector3 (10,0,5);
 }

 void Update () {
     if (Input.GetKeyDown (KeyCode.LeftControl)) 
     {
             StopCoroutine ("Firing");
             StartCoroutine ("Firing");
         //Debug.DrawRay (transform.position, transform.forward, Color.red);
     }
 }

 IEnumerator Firing()
 {
     laser.enabled = true;
     while (Input.GetKey(KeyCode.LeftControl))
     {
         Ray ray = new Ray (transform.position, transform.forward);
         RaycastHit hit;
         laser.positionCount = 10;
         laser.SetPosition (0, ray.origin);

         if (Physics.Raycast (ray, out hit, 100))
         {
             laser.SetPosition (1, hit.point);
             Debug.Log (hit.point);
             if (hit.collider.tag == "Mirror")
             {
                 laser.SetPosition (2, hit.point + 100.0f * Vector3.Reflect (transform.forward, hit.normal));
             }
         }
         else
             laser.SetPosition (1, ray.GetPoint (100));

         if (Input.GetKeyDown (KeyCode.Space))
             break;
     }
     yield return null;
     laser.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

Answer by Bunny83 · Jun 15, 2017 at 06:07 PM

The reason why you have an infinite loop is:

  • Input is only changed between frames. So as long as you don't let the current frame finish the input state won't change.

  • Your while loop only exits when the input state changes. Either when LeftControl is no longer held down, or when you press space.

  • However since you don't yield inside your while loop, you are stuck in that while loop.

You have to put your yield return null; inside your while loop. That way the coroutine is interrupted at that point which let the current frame finish and let the input state update.

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 TheRockRipper · Jun 24, 2017 at 11:01 AM 0
Share

@Bunny83 Thanks man. That was useful!

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

69 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

Related Questions

Is there a better way to find where to start my raycast? 2 Answers

Laser Beam using Raycasting & Line Renderer 1 Answer

Scripting Issues on the Player with Laser 2 Answers

javascript: declaring a variable in 'while' loop 2 Answers

unity bug??? while loop is satisfied, but doesn't quit 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