• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Sardio · Sep 11, 2020 at 12:15 AM · line

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sardio · Sep 11, 2020 at 01:19 PM 0
Share

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

alt text

screenshot-3.png (38.2 kB)
screenshot-4.png (45.5 kB)
avatar image streeetwalker Sardio · Sep 11, 2020 at 02:42 PM 0
Share

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

avatar image Sardio streeetwalker · Sep 11, 2020 at 04:23 PM 0
Share

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

Show more comments
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sardio · Sep 12, 2020 at 04:21 PM 0
Share

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

avatar image ransomink Sardio · Sep 13, 2020 at 03:51 PM 0
Share

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.

avatar image Sardio ransomink · Sep 13, 2020 at 05:35 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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.



Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Line Renderer with the pattern of a sprite 1 Answer

How to draw a line between two objects. 1 Answer

Infinite Scrollview and Read A File Line By Line 0 Answers

A line between two objects with conditions 0 Answers

Vectrocity VectorLine Method not Making Multiple Points Line With List 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges