Check if a point crossed a "line" - line and point moving

Disclaimer: I am new to unity and i am fooling around with a small first prototype

I am making a small game 2d where you have to shoot a ball through goals.
The goals are given by a line between pairs of markers and these markers are moving (changing the orientation of the goal, changing the size of the goal).

My question is how to check if the ball crossed a given line (bonus - the line is “moving”, the markers should be able to accelerate slow down, it would be perfect if they could be given by the line between rigid bodies).

Is this possible? And how would i approach it.

What i have at the moment is the ball (3d sphere, rigid body, sphere collider, physics material) and i am shooting the ball by mouse down, drag, let lose. The ball bounces around and the goal markers are given by other bodies (also spheres, sphere collider, rigid body) and i am moving the goal markers around.

Thanks for helping a beginner out,
richard

1 Answer

1

One way is to use a collider. Pick any game object that fits inside your goal. Make this object a child of your goal. Sounds like a box or plane will work. Remember planes are one-sided. After you have it positioned, select it and 1) turn off the mesh renderer by clearing the check box just to the left of “Mesh Renderer” and 2) under the collider section, select “Is Trigger”. Now you can use OnTriggerEnter() to make the score.

If you really want to use some sort of line calculation, it can be done. I’d need a bit more information about the layout of your game to help you.

thanks a lot for your help - i will try to create a plane and collide against it.

I got it to work with a variation of your code snippet and small capsule colliders. I use a collision layer for the capsules and let them collide with a small ball inside of the real ball. The capsules are aligned between the goals. It works perfectly. (Planes where too expensive)