# if touching line renderer (do something)

I have this laser script for a ship and I am trying to make it kill an enemy when it is hit with the line rendered laser. How can I achieve this. All things I found online I just could not get to work. An outline of the code would be (this isnt real code).

public gameobject linerenderer;

void update()
{
if (touching gameobject.linerenderer)
{
die
{
}

 private LineRenderer lr;
void Start()
{

lr = GetComponent<LineRenderer>();
}

void Update() // consider void FixedUpdate()
{

lr.SetPosition(0, transform.position);
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.collider)
{
lr.SetPosition(1, hit.point);
}
}
else lr.SetPosition(1, transform.forward * 5000);

}

However there is an anoying bug with this code... Before:

After you play the game for about 30 secs:

Settings:

• I did that but the ship movement script gets tons of drag and starts spazzing about when instantiating such objects or enabling and disabling but thanks for the help – Titaniumammas69 Jun 26 at 8:06
• Usually laser collisions are handled by raycasting. Have you checked through existing tutorials for implementing lasers for some starting points? – DMGregory Jun 26 at 11:00
• You probably discovered that code isn't very legible in comments. You should edit your question if you want to share more information about what you've tried or what you need. – DMGregory Jun 26 at 11:18
• Yes... I got a script from a tutorial that has this detection on it and scraped the original script from when I posted this question. There is a bug though which is it drifts after a while. Ill post the errors and script on the main question – Titaniumammas69 Jun 26 at 11:22
• yeah code isnt legible... I deleted it when it didnt work – Titaniumammas69 Jun 26 at 11:23

LineRenderer is not your friend here. While you can somehow make it work (you would need to dynamically create and update the colliders by code), that is not something you want to do. (As reference https://answers.unity.com/questions/470943/collider-for-line-renderer.html)

If you want to use your laser to kill something, it is just a bunch of stretched shiny bullet packed together. All you need to do is create a small laser prefab with an image of your laser and you spawn them in quick succession. Each of those laser bullets can have their own collider and therefore your collision/ die/ kill etc call.

With a bullet prefab, you can for example easily move your ship, continue shooting and you don't care about already shot bullets. The line renderer would just stay where it is. You will have a really hard time to update it to match your movement pattern of the ship.

1. instantiate a prefab should have no influence on your ships movement, if it does, you do it wrong. That would be a different problem and can only be helped if you post your implementation of it.
2. You don't enable/ disable bullet prefabs. You instantiate them and usually have an offscreen collider that destroys them. Upgraded version is a pool manager for them but for starters offscreen destroyer will do.
3. If you really want to use a linerenderer, you could take a look at raycasting and if that is an option for you. If the raycast hits, so should your laser if you use the same direction of it.
• Before using the laser I used bullets. But just making bullets is a pain and it messes up the ships flying patterns if it has an rb. I have used bullets before and it didnt work. I chose to use the line renderer since it didnt alter the ship since it has no rb and It works really well. I just need to complete the final step of making an enemy die when it is touching it. Also firing things in rapid succesion may be a tad cpu consuming. I have seen people online succeed in such areas and your idea is good but not for this context. I really appreciate the answer though and upvoted it – Titaniumammas69 Jun 26 at 8:26
• ps: since im on low rep when I upvote something it doesnt show – Titaniumammas69 Jun 26 at 8:28
• if we are talking about millions of bullets, that would be maybe be true. But your ship would have, depending on your length of the bullet, something between 10 to 50 prefabs at once. That is nothing to worry about at all. Bullets and the ship should be independent and not having any influence on each other -> wrong implementation/ setup if it does. Just be careful comparing what you see. Just because it looks like a straight line in a game does not mean the implementation is a linerenderer. – Zibelas Jun 26 at 8:45
• I respect your proposition and the view that you are taking on to fixing this but I just want to stick with the line renderer. – Titaniumammas69 Jun 26 at 9:18
1. Get the array of points from your line renderer.
2. In cycle, iterate these points, doing raycast from one to another.
3. If something met in raycast - kill it. Or interrupt cycle, remove further points, and put it back to line renderer, if you met something indestructible. In this way you can even implement reflecting laser (use vector3.reflect).

The cycle is something like this (pseudocode):

 for(int i= 1; i < points.Length; ++I)
{
Vector3 origin = points[i-1];
Vector3 direction = points[i] - origin;
float distance = origin.magnitude;

if ( Do standard raycast)
{
Destroy (hit.collision.collider.gameobject);
Break;
}
}

• yes. Good observation, do you know how this could be written in c# though? – Titaniumammas69 Jun 28 at 0:18