- Home /
Implementation of directional buildings?
This is a working part of my code, but I'm sure that there's a cleaner way to achieve this. The idea is that there's a grid and the code checks the grid for where its edges are. The walls are then placed on the edges of the grid with the correct rotation. It involves a combinations of walls too, such as: North + West, North + East, etc...
Is there a more efficient way to do this?
Answer by DCordoba · Mar 28, 2021 at 02:56 AM
amm, each edge false
need to have a wall right?
well, this support single and double (corner) wall types, Will be particularly difficult expand it to tree walls
1 wall uses a angle multiplier angleBy90
, 2 wall is hardcoded like you did, but using code determinism to not count double cases.
void Build () {
//how many walls are? just one or two
int wallCount = 1;
//wall angle multiplier is, north = 0f, east = 1f, south = 2f, west = 3f
float angleBy90 = 0;
if(!north){
wallCount = 1;
}
if(!east){
if(wallCount == 0){
angleBy90 = 1f;
}else{
//only one state can reach here, north-east corner
InstantiateCornerWall(180f);
}
wallCount++;
}
if(!south){
if(wallCount == 0){
angleBy90 = 2f;
}else{
//only one state can reach here, south-east corner
InstantiateCornerWall(270f);
}
wallCount++;
}
if(!west){
if(wallCount == 0){
angleBy90 = 3f;
}else{
//two states can reach here, south-west and north-west corners
if(mathf.approximately(angleBy90, 0f))//north-west
InstantiateCornerWall(90f);
else //south-west
InstantiateCornerWall(0f);
}
wallCount++;
}
if(wallCount == 1)
InstantiatePlaneWall(angleBy90);
}
//separate methods to instantiate each type of wall
void InstantiatePlaneWall(float angleBy90){
GameObject tempWall = Instantiate(myScript.wall, transform.position, Quaternion.identity) as GameObject;
tempWall.transform.Rotate(0f, 90f*angleBy90, 0f);
tempWall.transform.translate(0f, 0.75f, 0.45f);
tempWall.transform.parent = transform.parent;
}
void InstantiateCornerWall(float angle){
GameObject tempWall = Instantiate(myScript.cornerWall, transform.position, Quaternion.identity) as GameObject;
tempWall.transform.Rotate(0f, angle, 0f);
tempWall.transform.translate(0f, 0.75f, -0.45f);
tempWall.transform.parent = transform.parent;
}
please next time paste the code, using a image makes the risk to have typos in variables, and one have to copy by hand the code in order to refactorize it
Thank you so much. This looks great and very clear! Sorry about the image, I didn't think of that the impact at the time...
I guess that it's best to avoid triple walls as much as possible in that case? Doubles can already achieve enough as it is.
Thank you once again!
Your answer
Follow this Question
Related Questions
Are nested lists bad? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
One big script or lots of small ones? 0 Answers
How many objects is too many? 2 Answers