Wednesday, September 14, 2022
HomeGame DevelopmentC# Unity 3D - Decide if goal is inside an space, raycasting,...

C# Unity 3D – Decide if goal is inside an space, raycasting, angle sectors, boxcast?


I’ve a ship that may shoot targets, however cannons on the fitting aspect ought to by no means attempt to shot at targets on the left aspect of the ship. Thus I’ve created sectors utilizing the SignedAngle perform, this works fantastic for testing, however its additionally form of damaged as you may see from the visualization under.
I’ve tried utilizing boxcast however alas it additionally doesnt work for this use case.

img1

The above picture visualizes what my script does, however this isn’t an answer to my drawback. As targets near the aspect and entrance of the ship might be outdoors the sector, for clarification what I imply, see image 3.
img2

This second picture exhibits what occurs once we enhance the angle, we will now detect extra targets, however now we have 2 large incorrect sectors marked in purple which shouldnt be there.
img3 Correct

Lastly, that is how I feel it ought to look, its nonetheless a cone, however the large distinction is that it begins with a large backside, thus it resolves the issue Im having with the present SignedAngle perform which determines every part from a single level within the center.

That is the script for assigning targets to the proper checklist in response to which sector they’re in:

foreach (Rework goal in EnemyListManager.occasion.enemyShips.ToArray())
        {

            if (Vector3.Distance(rework.place, goal.place) > ship.mainGunCaliber.vary)
                proceed;

            Vector3 toTarget = goal.place - rework.place;
            print(Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up));

            if (Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) >= bowMinAngle &&
                Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) <= bowMaxAngle)
            {
                if (!bowTargets.Incorporates(goal))
                {
                    RemoveFromOthers(goal);
                    bowTargets.Add(goal);
                    print("added goal to Bow");
                }
                proceed;
            }
            if (Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) >= sbMinAngle &&
                Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) <= sbMaxAngle)
            {
                if (!sbTargets.Incorporates(goal))
                {
                    RemoveFromOthers(goal);
                    sbTargets.Add(goal);
                    print("added goal to SB");

                }
                proceed;

            }
            if (Vector3.SignedAngle(-hullParent.ahead, toTarget, Vector3.up) >= aftMinAngle &&
                Vector3.SignedAngle(-hullParent.ahead, toTarget, Vector3.up) <= aftMaxAngle)
            {
                if (!aftTargets.Incorporates(goal))
                {
                    RemoveFromOthers(goal);
                    aftTargets.Add(goal);
                    print("added goal to Aft");

                }
                proceed;
            }
            if (Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) >= psMinAngle &&
                Vector3.SignedAngle(hullParent.ahead, toTarget, Vector3.up) <= psMaxAngle)
            {
                if (!psTargets.Incorporates(goal))
                {
                    RemoveFromOthers(goal);
                    psTargets.Add(goal);
                    print("added goal to PS");

                }

            }
        }

Any assist can be appreciated on the way to deal with this drawback!

Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments