Another problem of scripting and geometry !

I need to detect when a ball crosses a line (raycast + line renderer, for now) that will be between two points (like a soccer goal) … but these points will move around the screen … How can I do to detect that he crossed the line?

Any solution that opens my mind will be of help to me

Thanks !!!

I thought to put a flag for when the raycast touches the ball, and if dsp stops touching it and the flag is enabled to count the goal … but it could happen that it touches the line rebound with one of the clubs and stops touching the line … in this case I would count as a goal without having crossed the line …

Is it like a soccer goal that moves? if that’s the case, you could put the raycast a bit inside the goal, in a position that would only trigger when the ball enters the goal enough to be considered a goal.

1 Like

Using a trigger area might be better. Just move the trigger object around, or if you need goals are preset locations just make multiples and turn them off and on as needed.

1 Like

Laiken : Thanks for your reply. The problem is that the goal line should be able to cross in both directions (that’s what I want to do). In case of doing so, you should do a raycast parallel to the raycast of the goal line only a few centimeters further back (or later, depending on where the ball comes from) … how could the parallel be created by working with vectors?

Lysander : Thanks for your reply. I would like to be able to do an area, it would be easier to detect with an OnTriggerEnter for example … but the clubs of the arc will move randomly, then the distance between the clubs will change, as well as the angle in which the goal line will be drawn …

Take a look at the image I uploaded. c1 and c2 are the clubs of your goal. l1, l2, l3 are lines that represent the raycast between the clubs and points. p1, p2, p3 and p4 are just points and should be made child objects of c1 or c2 (you choose). Since they are children of one of them, they should keep their offset position when c1 and c2 move. Make a raycast between p1 and p2 and another between p3 and p4, as shown by the leftmost picture.

You will know when a goal happens when the ball crosses l1>l2>l3 or l3>l2>l1, as shown by the 2 rightmost pictures.

1 Like

I will try this today ! I hope it works ! I’ll tell you later here

Thank you very much anyway!!!

But what happens if the bow stick rotates? the gameobject children rotate with it… What I need is to create parallel to the main raycast at a certain distance apart each

Can you help me Laiken ?

Sure. By bow stick you mean the clubs right? If you upload your scene + scripts here so that I can see exactly what is going on I can take care of the raycast part for you.

1 Like

Yes ! The clubs…
What happens if one of the soccer clubs rotates (it could happen in my game idea) … it would have to be parallel to the main raycast, which is the one that goes from the center of a club to the center of the other club

Thanks a lot for help me!

Any idea for this ?

If you don’t want the points to rotate, you can control their position by script instead of making them children of the clubs. But I think you need them to rotate when the clubs rotate for the lines to stay parallel(btw, make c2 be a child of c1 too). The only problem with making them children is if even the distance between c1 and c2 can change (like a goal that could become bigger or smaller as the game runs). Then another approach would be needed

1 Like

Here I upload an example…

If the clubs rotate I need to do something to create parallels of the red raycast line !!

3365630--263728--Example.jpg

Yes ! The distance between c1 and c2 could be change ingame… because the clubs will be move & rotate randomly…:frowning:

I see, then forget about what I said about making c2 child of c1. Let me think a bit here and I will soon give you the solution (if I don’t figure it out I will tell you too)

1 Like

THANKS A LOT Laiken !!

Im trying too !

I think I know how to do it. Is it a 2d or a 3d game? also, let me see the code you are using to make the raycast between the 2 clubs

1 Like

Its a 2D game :

            RaycastHit2D hit = Physics2D.Raycast(club1.transform.position, club2.transform.position);
            Debug.DrawLine(club1.transform.position, club2.transform.position, Color.red);

            if (hit.collider != null)
            {
                //In
            }

Alright. When I finish I will post the code here. I’m also cooking some stuff here so it may take a bit of time

1 Like

Ok… no prob…

Im trying to use Vector2.Angle right now…but I dont know if I can do it using this

Thanks !!!