- Home /
How do I make wall blocks automatically connect with adjacent blocks?
I'm making a 2D games, I'm trying to make walls with outline. The level will be generated from data in texts so I need it to be made automatically
How do I make it so, for example, if there are 3 wall blocks next to each other (picture), they'll automatically connect, and change their sprite to the correct one?
The blocks are placed in grid
Answer by RadonRaph · Oct 09, 2019 at 10:54 AM
Hello @cdr9042, one way is to cast 2 ray (one up one left) and save if it have hit an object or not and the reference to the object. Then wait for a frame (time to do all the raycast) then try if one of the object that have been hitten have himself hit an object etc for each object hitten add hit to an array! when is done the position of the first object is equal to his position + (array.length/2) and size = size+array.length and you destroy each object of the array. I've done this in a game this week and it work absolutly perfect here is my code:
IEnumerator Start()
{
yield return new WaitForEndOfFrame();
transform.position = new Vector3(Mathf.Round(transform.position.x), transform.position.y, Mathf.Round(transform.position.z));
RaycastHit up;
Physics.Raycast(transform.position, new Vector3(0, 0, 1), out up, 0.5f);
RaycastHit left;
Physics.Raycast(transform.position, new Vector3(1, 0, 0), out left, 0.5f);
if (up.transform)
upObj = up.transform.gameObject;
else if (left.transform)
leftObj = left.transform.gameObject;
yield return new WaitForEndOfFrame();
List<GameObject> ups = new List<GameObject>();
List<GameObject> lefts = new List<GameObject>();
GameObject target;
if (upObj){
target = upObj;
while (target != null)
{
ups.Add(target);
if (target.GetComponent<Unit>().upObj)
{
target = target.GetComponent<Unit>().upObj;
}
else
{
target = null;
}
}
int size = ups.Count;
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + size/ 2f);
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z + size);
for (int i = 0; i < size; i++)
{
Destroy(ups[i]);
}
}
else
{
if (leftObj)
{
target = leftObj;
while (target != null)
{
lefts.Add(target);
if (target.GetComponent<Unit>().leftObj)
{
target = target.GetComponent<Unit>().leftObj;
}
else
{
target = null;
}
}
}
int size = lefts.Count;
transform.position = new Vector3(transform.position.x + size / 2f, transform.position.y, transform.position.z);
transform.localScale = new Vector3(transform.localScale.x + size, transform.localScale.y, transform.localScale.z );
for (int i = 0; i < size; i++)
{
Destroy(lefts[i]);
}
}
}
This script work only with full horizontal or vertical but i think you can tweek it a little to make it like you want ! Hope that help ! Raph
Answer by tormentoarmagedoom · Oct 09, 2019 at 09:31 AM
Hello there.
I think best way is to create a "detector" using trigger colliders in each 4 sides so you can know when there is another block there so you can change the sprite. Each block must be created with its own 4 detectors.
Once you detected the block and cnaged the sprite, delete the detector.
Bye!
Thank you for the answer. I think managing it only with code would have better performance.
I guess I'll do something similar by checking for adjacent blocks from a List
ITs ok then, but if the list is too large, it will reduce performance. Using Colldiers is best way to optimize resources.
Your answer
Follow this Question
Related Questions
FixedJoints wiggling when too many are put together? 1 Answer
how to make a player die on collision 2 Answers
Making My Character Stop When It Hits A Wall 2 Answers
Creating A wall Jump 0 Answers