- Home /
laser is not working
I have a script that I have been working on for a while now. I have it almost done and ready to go. I have it so that it has multiple if statements in it so I can target different items and have it so that different things will happen to each item, or so I hope. It is not working out that way. When I fire the laser it goes toward but not directly at the object tagged Drone. It will not fire in any other direction. I need to know why that is. Please look over my script and tell me what is wrong with it. Thank you. using UnityEngine; 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;
}
}
}`
Your answer
![](https://koobas.hobune.stream/wayback/20220613135738im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to assign a tag to multiple parts of a Model 0 Answers
Can't get more than one object. 1 Answer
Add script to selection 2 Answers
One script, multiple object texture change 1 Answer
Can I use Multiple tags on if? 1 Answer