• 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
Question by schult777 · Mar 07, 2014 at 08:06 AM · tagsdifferenttargets

Multiuse laser beam

I have a laser beam script that works great. The only problem I am having with it is that I can only affect rigid body objects. I want it to be able to push on rigid body objects [which it is doing right now] and then also destroy the targets labeled enemy. I also want it to destroy other items in the level that have different tags on them. Where in my scripting do I place the coding for it and what would it look like? Please help me I need this script to be able to pass my college class. I need to pass this class to graduate. `using UnityEngine; using System.Collections;

public class Laserbeam : MonoBehaviour { LineRenderer line;

 void Start () 
 {
     line = gameObject.GetComponent<LineRenderer>();
     line.enabled = false;
 }
 
 
 void Update () 
 {
     if(Input.GetButtonDown("Fire1"))
         
     {
         StopCoroutine("FireLaser");
         StartCoroutine("FireLaser", 0.25F);
         audio.Play();
         
     }
 }
 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

People who like this

0 Show 5
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 robertbu · Mar 07, 2014 at 08:29 AM 1
Share

I'm troubled by your question. If you wrote this script, then the changes you would need to make would be clear. If you borrowed this script, aren't we doing your school work for you? This is script is not that hard to understand. All the classes and functions used here are common and well documents in the reference. And there are a multitude of answers on UA concerning the functions and concepts in this script. Don't you think you should make an effort to go through and understand this script and make the changes on your own? Shouldn't you be turning in your own work for your classes?

avatar image schult777 · Mar 08, 2014 at 07:57 PM 0
Share

I learned how to do the basic script via a tutorial. I figured out how to add the sound on my own. I just want a good place to learn how to put multiple if and else statements into this script. I have searched the web but have not found any non confusing sections on it. I just need a good direction to go.

avatar image schult777 · Mar 08, 2014 at 10:34 PM 0
Share

Alright I now have multiple if statements in my script. I found my answer but a new problem has come up. when I fire my laser it goes toward one of the targets but now to where I was aiming it. I have a target tagged Drone and the laser beam always heads toward it. Please tell me how to fix it. I now have multiple if statements in my script. using System.Collections;

 public class Beamlaser : MonoBehaviour 
 {
     LineRenderer line;
     public GameObject other;
     public GameObject explosion;
 
     void Start () 
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
     
     
     void Update () 
     {
         if(Input.GetButtonDown("Fire1"))
             
         {
             StopCoroutine("FireLaser");
             StartCoroutine("FireLaser", .25F);
             audio.Play();
             
         }
     }
     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.collider.gameObject.tag == "Hand")
                 {
                 Instantiate(other, hit.point, transform.rotation);
                 Destroy(hit.transform.gameObject); 
                                 }
             }
             else if(hit.collider.gameObject.tag == "Drone")    
             {
                 Instantiate(explosion, hit.point, transform.rotation);
                 Destroy(hit.transform.gameObject);
                     line.SetPosition(1, ray.GetPoint(1000));
             
             yield return null;
         }
         line.enabled = false;
         }
     }
 }
avatar image schult777 · Mar 08, 2014 at 11:20 PM 0
Share

WARNING! RUNNING THE ABOVE CODE WILL CRASH UNITY! I have run and rerun this code multiple times and every time it is crashing unity. So run this code at your own risk.

avatar image robertbu · Mar 08, 2014 at 11:36 PM 0
Share

Yea, it will crash Unity. The issue is that line 53:

  yield return null;

...needs to be executed every time through the while() loop. The way you have it now, it will only be executed when this line is executed and true:

   else if(hit.collider.gameObject.tag == "Drone") 

You have bracketing issue with your logic. Get all your brackets to line up correctly and look at your logic. In Monodevelop, if you place your cursor at one bracket, the matching bracket will be highlighted.

Note you've also migrated this line inside your while() loop:

    line.enabled = false;

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Semi-Controlled Array Randomization 1 Answer

move between unique tags 0 Answers

Are Tags the best solution for this? Duplicating an item that duplicates itself a set amount of times. 1 Answer

Find Script from another script 2 Answers

Question about mipmapping and downscaling 0 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