- Home /
Question by
loizos-95b · Dec 02, 2017 at 07:47 PM ·
gameobjectinstantiateraycastupdatespawn
How should i add raycast to multiple game objects???
Hello coders, I would like to know some possible ways to create the same raycast for multiple game-objects. I want to use raycast to check if two obstacles(walls) are colliding and if yes, change the position of the wall to avoid obstacles collisions. Here is the script that is attached on a empty gameobject (not movable) in the terrain :
public class Spawn_Objects_Script : MonoBehaviour {
/* Get Original Prefabs */
public GameObject Original_wallPrefab;
/* Declare unrecognizable (Missing Objects) */
public GameObject PlayerPrefabForClones;
/* Declare a number of spawnings for each prefab */
public static int numberOfWalls = 10;
/* Create a new group for each object */
public static GameObject[] WallsGroup = new GameObject[numberOfWalls];
/* Create an array of Game Objects to hold the Prefabs */
public GameObject[] MySpawningPrefabs;
private Vector3 position;
[HideInInspector]
public Vector3 RaycastStartPosition;
void Start () {
StartCoroutine("SpawnWorld");
}
void Update()
{
//Raycast(RaycastStartPosition)
}
IEnumerator SpawnWorld()
{
//Wait for 2 seconds every time you spawn something
WaitForSeconds wait = new WaitForSeconds(2.5f);
for (int i = 0; i < WallsGroup.Length; i++)
{
//position = AvoidObstaclesCollition();
position = new Vector3(Random.Range(300, 600), 155, Random.Range(235, 710));
WallsGroup[i] = Instantiate(Original_wallPrefab, position, Quaternion.identity) as GameObject;
RaycastStartPosition = new Vector3(position.x, position.y + 5f, position.z);
Raycast(RaycastStartPosition);
}
yield return wait;
}
private Vector3 AvoidObstaclesCollition()
{
while (true)
{
position = new Vector3(Random.Range(300, 600), 155, Random.Range(235, 710));
RaycastHit hit;
if (Physics.Raycast(position, -Vector3.up, out hit, Mathf.Infinity))
{
Collider[] objectsHit = Physics.OverlapSphere(position,10, 0);
//.OverlapBox(position, 10, null);
if (objectsHit.Length == 0)
{
return position;
}
}
}
}
void Raycast(Vector3 RaycastStartPosition)
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 100;
Vector3 back = transform.TransformDirection(Vector3.back) * 100;
Vector3 left = transform.TransformDirection(Vector3.left) * 100;
Vector3 right = transform.TransformDirection(Vector3.right) * 100;
Debug.DrawRay(RaycastStartPosition, forward, Color.red);
Debug.DrawRay(RaycastStartPosition, back, Color.green);
Debug.DrawRay(RaycastStartPosition, left, Color.yellow);
Debug.DrawRay(RaycastStartPosition, right, Color.cyan);
}
}
Solution 1: Call the Raycast method in the void Update() ------> Issue: Create a raycast only at the last wall(10th wall)
Solution 2: Call the Raycast method in the IEnumerator inside the for loop ------> Issue: Create the raycast only at the first frame(Visible only when the pause is pressed before the play Mode)
for (int i = 0; i < WallsGroup.Length; i++){
. . .
RaycastStartPosition = new Vector3(position.x, position.y + 5f, position.z);
Raycast(RaycastStartPosition);
}
IT WILL BE GREATLY APPRECIATED IF YOU HAVE ANY SOLUTION ON MIND JUST LET ME KNOW, THANKS FOR YOUR TIME.
update.png
(297.4 kB)
forloop.png
(251.9 kB)
Comment