Get object which collide with physics.spherecast and displace to opposite direction

Hello there,
from the main class, on awake I run a method which place randomly some objects on a NavMesh.
Now the problem is that this object get overlapped.

To avoid the problem I wish to cast a Spherecast to check who is the guilty and move the new instanced object on the Opposite direction plus a float Gap which I wish to add to separate the object.

Here is the actual code…

void PlaceEnergyPool()
	{
		EnergyPools.Clear();

		foreach (var p in Players)
		{
			for (int i = 0; i < (MaxEnergyPool / Players.Count); i++)
			{
				GameObject energyPoolPrefab = Instantiate(EnergyPool);
				Vector3 randomPos = GenerateNavMeshRandomPoint(NavMeshToMesh());
				
				float desiderDistance = energyPoolPrefab.GetComponent<NavMeshObstacle>().radius + EnergyPoolsGap;
				if (Physics.CheckSphere(transform.position, desiderDistance, 0))
				{

					foreach (var ep in EnergyPools)
					{
						RaycastHit rhit;
						if (Physics.Raycast(randomPos, ep.transform.position, out rhit))
						{
							if (rhit.distance <= desiderDistance)
							{
								Vector3 rhitDir = ep.transform.position - randomPos;
								randomPos += -rhitDir;
							}
						}
					}
				}
				energyPoolPrefab.transform.position = randomPos;
				EnergyPools.Add(energyPoolPrefab);
			}
		}
	}

Help Help ! :slight_smile:

What I need it to know who is inside of the sphere and in which direction, so I can move the random placed object a bit more distant… shouldn’t be so hard >.<

Ok… I’ve roughly made it works…

foreach (var p in Players)
		{
			for (int i = 0; i < (MaxEnergyPool / Players.Count); i++)
			{
				GameObject energyPoolPrefab = Instantiate(EnergyPool);
				energyPoolPrefab.name = "EnergyPool(" + i.ToString() + ")";
				
				float desiderDistance = EnergyPoolsGap + (energyPoolPrefab.GetComponent<NavMeshObstacle>().radius * 2);
				float gapX = ((MaxX / Players.Count) / MaxEnergyPool) + desiderDistance; Debug.Log("GapX = " + gapX);

// This is a complex method which create a mesh = to navmesh and check triangle per triangle if the vector selected is inside the navmesh
				Vector3 candidatePos = GenerateNavMeshRandomPoint(NavMeshToMesh());
				Vector3 rndPos = candidatePos;

				NavMeshPath path = new NavMeshPath();
				if (!NavMesh.CalculatePath(candidatePos, p.Location.transform.position, NavMesh.AllAreas, path))
				{
					if (!RandomPoint(candidatePos, desiderDistance * 2, out rndPos)) // this will change rndPos
					{
						float temp = Random.Range(-(gapX * i), gapX * i);
						Debug.Log("RandomPoint faild, so we use candidatePos (" + candidatePos + ") 
" + temp);
						rndPos = new Vector3(candidatePos.x + temp, 0, candidatePos.z);
					}
				}

				if (i >= 1)
				{
					foreach (var crater in EnergyPools)
					{
						// X
						if (Mathf.Abs(candidatePos.x - crater.transform.position.x) < gapX)
						{
							// If the player position is near over the half of the terrain ... we need to decrease the X !
							if (p.Location.position.x < MaxX / 2)
								candidatePos = new Vector3(p.Location.transform.position.x + (gapX * i), 0, rndPos.z);
							else
								candidatePos = new Vector3(p.Location.transform.position.x - (gapX * i), 0, rndPos.z);
							//Debug.Log("Player:" + p.Location.transform.position.x + ", " + energyPoolPrefab.name + ":" + candidatePos + "
 (desPos:" + (gapX * i) + ")");
						}

						Debug.Log("Comparing Z axis "+ crater.name +"(" + crater.transform.position.z +") with "+ candidatePos.z);
						// Z
						if (Mathf.Abs(candidatePos.z - crater.transform.position.z) < desiderDistance)
						{
							if (crater.transform.position.z < (MaxZ / 2))
								candidatePos = new Vector3(candidatePos.x, 0, crater.transform.position.z + desiderDistance);
							else
								candidatePos = new Vector3(candidatePos.x, 0, crater.transform.position.z - desiderDistance);
						}
					}
				}

				if (!NavMesh.CalculatePath(p.Location.transform.position, candidatePos, NavMesh.AllAreas, path))
					candidatePos = new Vector3(candidatePos.x, 0, p.Location.position.z);
				
				energyPoolPrefab.transform.position = new Vector3(candidatePos.x, 0, candidatePos.z);
				EnergyPools.Add(energyPoolPrefab);
			}