- Home /
 
               Question by 
               Codelyy · May 16, 2016 at 08:07 PM · 
                c#linerendererstatelaser beam  
              
 
              Line Renderer works only once
I'm using a Line renderer to create a channelled Laser beam going from sort of under the enemy firing the laser beam to the top of the screen. It looks like this: 
It works fine as you can see but if I want it to fire the laser again in the same session, it will play the animation but the laser won't appear and I'm not sure why.
This is the script I used to create the laser:
 void LaserBeam()
     {
         anim.SetBool("Aggro", true);
         anim.SetTrigger(laserBeamHash);
 
         if(!anim.GetCurrentAnimatorStateInfo(0).IsName("LaserBeam"))
         {
             transform.position = new Vector3(transform.position.x + 0.1f * Time.deltaTime, transform.position.y, transform.position.z);
             return;
         }
 
         transform.position = new Vector3(transform.position.x - 0.3f * Time.deltaTime, transform.position.y, transform.position.z);
         laserBeaming = true;
         RaycastHit2D hit = Physics2D.Linecast(firePoint.transform.position, laserBeamPoint.transform.position, laserBeamLayerMask);
         lineRenderer.enabled = true;
         lineRenderer.sortingOrder = 2;
         lineRenderer.SetPosition(0, firePoint.transform.position);
         lineRenderer.SetPosition(1, laserBeamPoint.transform.position);
         canMove = false;
 
         if(!audioSource.isPlaying)
         {
             audioSource.clip = laserBeamSound;
             audioSource.Play();
         }
 
         if(laserBeamPoint.transform.position.x >= laserBeamPoint2.transform.position.x)
         {
             laserBeamPoint.transform.position = new Vector2(laserBeamPoint.transform.position.x - 0.4f, laserBeamPoint.transform.position.y);
         }
 
         if(laserBeamPoint.transform.position.x <= laserBeamPoint2.transform.position.x)
         {
             laserBeamPoint.transform.position = new Vector2(laserBeamPoint.transform.position.x, laserBeamPoint.transform.position.y + 0.4f);
         } 
 
         if(laserBeamPoint.transform.position.y >= laserBeamPoint3.transform.position.y)
         {
             //lineRenderer.enabled = false;
             audioSource.Stop();
             canMove = true;
             laserBeaming = false;
             lineRenderer.SetPosition(0, Vector3.zero);
             lineRenderer.SetPosition(1, Vector3.zero);
 
             //anim.SetBool("Aggro", false);
         }
 
         if(hit)
         {
             HealthManager.DecreaseHealth("Player", 30);
         }
     }
And how the function is called:
 case EyeBallStates.LaserBeam:
                 int positionSelecter = 0;//Random.Range(0, 2);
                 if(positionSelecter == 0)
                 {
                     transform.position = Vector3.MoveTowards(transform.position, movePoint1.transform.position, moveSpeed * Time.deltaTime);
                     if(transform.position.x >= movePoint1.transform.position.x)
                     {
                         transform.localScale = new Vector3(1, 1, 1);
                         LaserBeam();
                     }
                 }
                 else if(positionSelecter == 1)
                 {
                     transform.position = Vector3.MoveTowards(transform.position, movePoint2.transform.position, moveSpeed * Time.deltaTime);
                     if(transform.position.x <= movePoint2.transform.position.x)
                     {
                         transform.localScale = new Vector3(-1, 1, 1);
                         LaserBeam();
                     }
                 }
 
             break;
Does anyone know what the problem is?
 
                 
                stupid-images-2.jpg 
                (17.2 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to find the radius area of linerencderer drawn circle and move objects inside ? 1 Answer
Serialize a LineRenderer 2 Answers
Drawing line from plane edge 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                