Z
Z
Zefirot2021-07-07 15:20:38
Unity
Zefirot, 2021-07-07 15:20:38

How to determine that under one collider is another?

let's say I create a grid of prefabs that have colliders

for (float x = -25f; x <= 25f; x += 0.5f){
  for (float y = -25f; y <= 25f; y += 0.5f){
    CellX[Num] = Instantiate(objectX) as ObjectX;
    CellX[Num].transform.SetParent(ObjectBoardCells.transform);
    CellX[Num].transform.position = new Vector3(x, y, 1f);
    ++Num;
    }
  }

How to determine that the newly created collider does not "touch" some other previously created one, and if it does intersect, what kind of object is it? (which also has a collider)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnanasikDev, 2021-07-08
@Zefirot

In principle, there are 2 options:
1. Create a script, hang it on all the objects you create. In it, declare the OnCollisionEnter or OnTriggerEnter method. When using these functions, there is also access to the intersected object. From there you can already do everything you need. (I recommend this method)
2. Use one of the functions: Physics.Raycast, Physics.Spherecast, etc. Raycast allows you to check only one direction (more than one is possible, but there are other ways to do this). Spherecast allows you to get all
intersected objects within a given radius.
You also need to understand that working with colliders is working with physics, and for this you need the Rigidbody component (at least on one object from a pair, in your case it is better to have all of them). You can also turn off the use of gravity so that it does not interfere.
PS: the Instantiate function has the following overload: GameObject Instantiate(GameObject obj, Vector3 position, Quaternion rotation, Transform parent) which will combine 3,4,5 rows like this:

CellX[Num] = Instantiate(
                      objectX, 
                      new Vector3(x, y, 1f), 
                      objectX.transform.rotation, 
                      ObjectBoardCells.transform) 
                      as ObjectX;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question