# Create a line between 2 points and check for collision

So, I am making a game with 3 balls, you move one of them and then you can become another ball, and I want to create a line between the 2 inactive balls to check if the active ball touches it.Otherwise you lose the game.

My only question is how do I make a line between those 2 points and check for collision.

**NOTE** that the line needs to update every time a ball is moved so it will always go from **inactive ball 1** to **inactive ball 2**.

**Answer** by streeetwalker
·
Sep 11, 2020 at 11:58 AM

Hi @Sardio, the problem is relatively easy if you can do basic algebra-trigonometry (it's not that tough). Google, Find Intersection of a Line and a Sphere in 3D. You want to find the intersection with a line segment. Here is one source (scroll about 1/4 of the way down the page): http://paulbourke.net/geometry/circlesphere/

Because you know the endpoints of the line segment, and the position and size of the sphere you want to find the intersection with, it should be easy to implement.

No I need the line to be in 2d, here are some images that may help you understand

@Sardio, yes, I understood the general concept. If you are working in 2D the calculations are a little simpler, but essentially the same. And it is still pretty simple and much less demanding than doing colliders - well, more demanding on you, but I believe less demanding on the system.

One question is the scope of what the intersections you want to calculate. For example, you want to calculate whether any sphere intersections with a line between all possible sphere pairs?

It doesn't alter the intersection calculations, but how you go about arranging the code that will call the calculations, and how you handle the results.

I want to check for a ball between the 2 balls, like a collision, i wanted to make a line renderer but i don't think that can have a collider, so a physics check would work? if so how do i do it, i am relatively new to coding

**Answer** by ransomink
·
Sep 12, 2020 at 01:06 PM

If you know both points of the line you can simply use Physics2D.Linecast. Including that, either a Raycast or CircleCast will all work depending on how you want to detect the ball. Raycast is a laser shot in a direction over a distance (its length). Linecast is a line from start to end. CircleCast is a circle (ball size) shot in a direction over a distance (length).

Thank you! But now I also have a problem, if a draw a line from **inactive ball 1** to **inactive ball 2** the first thing it will hit will be **inactive ball 1** how do I make the line shorter to not touch the **inactive balls**

Yes, the Linecast method will detect colliders at the start of the line. To remedy this, you should use

LinecastAll: this will create an array of all colliders hit and add them in order based on distance, or...

LinecastNonAlloc: this does the same as LinecastAll *except* you provide the array (to avoid memory allocation) and it will fill it for you. Remember, this version will only fill the array based on the size you allocate for it. If you allocate a length of 5, it will only fill 5 objects, even if the Linecast hits more than 5. So make sure your length is the correct amount of objects you want to hit.

After the Linecast, you need to loop through the array and check if each collider hit is the inactive ball 1 and ignore it, to continue checking the other colliders.

How do I ignore? if the ball is inactive how do I ignore it?

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.