- Home /
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;
}
}
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?
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.
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 ai$$anonymous$$g 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 : $$anonymous$$onoBehaviour
{
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;
}
}
}
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.
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 $$anonymous$$onodevelop, 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;
Your answer
Follow this Question
Related Questions
Semi-Controlled Array Randomization 1 Answer
move between unique tags 0 Answers
Raycast that ignores object with certain tags? 2 Answers
Creating a dialogue with Physics.OverlapSphere 0 Answers
working with tags 2 Answers