How to damage an enemy with Line Renderer?

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.!

In your situation I would just have a public function in the enemy script to take damage, and check for the script with the raycast to call that function:

public void TakeDamage(float amount){
    startingHealth -= amount);
}

//Then call in the laser script

if(Physics.Raycast(ray, out hit, 100)){
    line.SetPosition(...);
    if(hit.rigidbody){
        ...
    }
    //Tag, name, check for component or something else
    if(hit.gameObject.GetComponent<Enemu>()){
        hit.gameObject.GetComponent<Enemu>().TakeDamage(someAmount);
    }
}

i’d just use the unity message system to notify the hit object about the event, like so

RaycastHit raycastHit;
var isHit = Physics.Raycast(...out raycastHit);
if(isHit) {
    raycastHit.collider.SendMessage("OnLazerHit", [optional parameter], SendMessageOptions.DontRequireReceiver);
}
  1. use raycastHit.collider instead of rigidbody because according to the documentation “rigidbody: The Rigidbody of the collider that was hit. If the collider is not attached to a rigidbody then it is null.” → so you will definitely have a collider, but possibly no rigidbody

  2. you can pass 1 optional parameter, maybe pass the reference to the lazer itself so you can query all the info you need, or create an extra LazerHitEvent class “DTO” or pass an integer as damage or w/e

  3. not all gameobjects that can be hit will implement the OnLazerHit method, so you don’t want to get an error if they don’t, thus tell unity that there need not be a receiver

  4. this is more flexible than checking the component of the hit object, because you could implement OnLazerHit wherever you want (maybe balloons will pop when hit with a lazer, or you can use it on a lock to melt it, etc.)

Thank you both for your help, it works now!