- Home /
Test line renderer collision?
Is there any way i could test if line render is collided with something because have this line of code to make it grow transform.localScale += new Vector3 (0.1F, 0, 0);
but when it collides with something the script will stop i know how to do that just not the collision part should i use raycast? so if you have any help i would really appreciate it! 2D C#
So whenever your gameObject's collider2D is intersected by a line described by a pair of Vector2s you would like the intersected gameObject's scale to change?
Answer by MissingL_tter · Aug 24, 2017 at 02:21 AM
The wording of this question is a bit confusing. From what a I can tell, you need some way to tell if the line rendered is going to collide with something as it extends.
If you can check ahead of time then yes, I would suggest using a Raycast.
You can Raycast a certain distance (if you know how far you are going to be extending the line), or simply only specify an origin and a direction and let it cast to Mathf.Infinity but this is a bit more expensive than making a finite cast.
If for some reason you don't know how far you are going to be extending the line then depending on what is happening in your script you have a couple of options:
Raycast a small distance from the end of the line to see if it will collide on the next physics update.
You can do a lot with Try-Catch blocks in situations like these where you aren't quite sure exactly what is going to happen.
I'm not sure what you mean by "the script will stop" but if there is an error surrounding the line extension in a Try-Catch might be the solution you are looking for. Keep in mind though, that this will get "tried" on every update if you let it and you should do something to prevent that if you can.