Hello everyone.
I’m trying to make a D&D mechanic, “free attack”. When a character leaves the zone of defeat, then a blow is carried out on him.This task is reduced to finding the intersection points of the segments of the path and the sphere. I did it. Here’s what happened.
https://youtu.be/7J6QIiAOfsM
The bugs that I corrected in the video, now everything works fine.
void OnEnable()
{
spisokSfer.Clear();
spisokSfer.AddRange( WarRegim.selectedObjectsWRheroy);
spisokSfer.Remove(gameObject);
for (int i = 0; i < spisokSfer.Count; i++)
{
krygiSvAt.Add(Instantiate(KeshPriStart.AtStStat, Vector3.zero, Quaternion.Euler(45, 45, 0)));
krygiSvAt.Add(Instantiate(KeshPriStart.AtStStat, Vector3.zero, Quaternion.Euler(45, 45, 0)));
}
for (int ii=0; ii<krygiSvAt.Count; ii++)
{
krygiSvAt[ii].SetActive(false);
}
}
List<GameObject> krygiSvAt = new List<GameObject>();
void OnDisable()
{
for (int i = 0; i < krygiSvAt.Count; i++)
{
Destroy(krygiSvAt[i].gameObject);
}
krygiSvAt.Clear();
}
List<Vector3> tochkiSf = new List<Vector3>();
void svobodnayaAttaka()
{
List<Vector3> buffr = new List<Vector3>();
ai2.GetRemainingPath(buffr, out bool stale);
tochkiSf.Clear();
for (int ii = 0; ii < spisokSfer.Count; ii++)
{
for (int i = 0; i < buffr.Count; i++)
{
if (i < buffr.Count - 1)
{
var heading = buffr[i + 1] - buffr[i];
float A = heading.x * heading.x + heading.y * heading.y + heading.z * heading.z;
float B = 2 * (buffr[i].x * heading.x + buffr[i].y * heading.y + buffr[i].z * heading.z - spisokSfer[ii].transform.position.x * heading.x - spisokSfer[ii].transform.position.y * heading.y - spisokSfer[ii].transform.position.z * heading.z);
float C = buffr[i].x * buffr[i].x - 2 * buffr[i].x * spisokSfer[ii].transform.position.x + spisokSfer[ii].transform.position.x * spisokSfer[ii].transform.position.x + buffr[i].y * buffr[i].y - 2 * buffr[i].y * spisokSfer[ii].transform.position.y + spisokSfer[ii].transform.position.y * spisokSfer[ii].transform.position.y +
buffr[i].z * buffr[i].z - 2 * buffr[i].z * spisokSfer[ii].transform.position.z + spisokSfer[ii].transform.position.z * spisokSfer[ii].transform.position.z - Personag.rangeWar * Personag.rangeWar;
float D = B * B - 4 * A * C;
float prom = (float)(Mathf.Sqrt(D));
float t1 = (float)((-B - prom) / (2.0 * A));
float t2 = (float)((-B + prom) / (2.0 * A));
Vector3 tochka1 = new Vector3();
Vector3 tochka2 = new Vector3();
tochka1 = new Vector3(buffr[i].x * (1 - t1) + t1 * buffr[i + 1].x, buffr[i].y * (1 - t1) + t1 * buffr[i + 1].y, buffr[i].z * (1 - t1) + t1 * buffr[i + 1].z);
tochka2 = new Vector3(buffr[i].x * (1 - t2) + t2 * buffr[i + 1].x, buffr[i].y * (1 - t2) + t2 * buffr[i + 1].y, buffr[i].z * (1 - t2) + t2 * buffr[i + 1].z);
if (D < 0)
{
// print("des");
}
else if ((t1 > 1 || t1 < 0) && (t2 > 1 || t2 < 0))
{
//print("des");
}
else
if (!(t1 > 1 || t1 < 0) && (t2 > 1 || t2 < 0))
{
//print(tochka1);
tochkiSf.Add(tochka1);
}
else if ((t1 > 1 || t1 < 0) && !(t2 > 1 || t2 < 0))
{
// print(tochka2);
tochkiSf.Add(tochka2);
}
else if (D == 0)
{
// print(tochka1);
// tochkiSf.Add(tochka1);
}
else
{
// print(tochka1);
// print(tochka2);
tochkiSf.Add(tochka1);
tochkiSf.Add(tochka2);
}
}
}
}
if (altBool == true)
{
for (int iii = 0; iii < krygiSvAt.Count; iii++)
{
if (tochkiSf.Count > iii)
{
print(tochkiSf[iii]);
krygiSvAt[iii].transform.position = tochkiSf[iii];
krygiSvAt[iii].SetActive(true);
}
else krygiSvAt[iii].SetActive(false);
}
}
else
{
for (int iii = 0; iii < krygiSvAt.Count; iii++)
{
krygiSvAt[iii].SetActive(false);
}
}
}
Spheres are my characters. Visually, I see the intersection points of the path and the spheres. But for several days now I can’t figure out how to use the code to find all the spheres through which the path lies and write all these spheres into the “List”. Or, best of all, Dictionary<GameObject, Vector3>, where Vector3 is the entry point to the sphere. That is, now we need all the spheres that have two points of intersection.
And one more question. There is an entry point to the sphere. How to write the “if” condition through which you can check whether the character has entered the sphere.
Help me please.