C#
So I have 2 scripts, one for my laser line renderer, which is this:
using UnityEngine;
using System.Collections;
//using System;
public class LazerScript : MonoBehaviour {
public ParticleSystem Col;
LineRenderer line;
public float Pew = 10;
//public float radius = 10;
//public float j = 20;
public AudioClip[] sounds; // set the array size and fill the elements with the sounds
void Start ()
{
line = gameObject.GetComponent<LineRenderer>();
line.enabled = false;
Screen.lockCursor = true;
}
void Update ()
{
if(Input.GetButtonDown ("Fire1"))
{
StopCoroutine("FireLazer");
StartCoroutine("FireLazer");
}
}
IEnumerator FireLazer() // IEnumerator is a coroutine return type. Continually fires while holding down the button
{
line.enabled = true;
audio.Play ();
audio.clip = sounds[Random.Range(0, sounds.Length)];
audio.Play();
while(Input.GetButton("Fire1")) //get button down only fires once. Get button fires when held down
{
Ray ray = new Ray(transform.position, transform.forward); //Transforms.position comes from the position of the object, transform .forward fires straight. or direction thing is facing
RaycastHit hit;
//audio.Play();
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, 100))
{
line.SetPosition (1, hit.point);
if(hit.rigidbody)
{
hit.rigidbody.AddForceAtPosition(transform.forward * Pew, hit.point);
Instantiate(Col, hit.point, Quaternion.identity);
}
}
else
line.SetPosition (1,ray.GetPoint(100)); // get at 100 units forward or from 100 units forward or the end
yield return null;
}
line.enabled = false;
audio.Stop();
}
}
Next, here is my enemy script which I so foolishly tried to make (the gameobject) be damaged by the line renderer as you see here:
using UnityEngine;
using System.Collections;
using System;
public class Enemu : MonoBehaviour {
private float startingHealth = 100;
public GameObject enemy;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(GetComponent(LineRenderer) = hit.point) // ????
{
startingHealth --;
}
if(startingHealth <= 0)
Destroy(this.GameObject);
}
}
My main problem is what the heck do I use in the enemu script to make the gameobject be damaged by the laser?
Of course the enemy script doesn’t work because I’m at a loss at what to do, I’m not new to all the bits and pieces used in C#, but I’m in no ways a fluent coder, which is why I am playing around with what I have learned, but this line renderer is throwing me for a loop… but lasers are so fun.
All help is appreciated.!