- Home /
Create a Dynamic Wall
Hi! I am new to unity, but not new to programming. I have a question about creating a dynamic scene;
I am following this tutorial (http://catlikecoding.com/unity/tutorials/maze/) and what i am trying to achieve is to send a JSON to my game from which i will create a house (floor, walls).
At this point i have the start point (x,y) of the wall and the end point(x,y).
private void CreateWall (Corner start, Corner end) {
MazeWall wall = Instantiate(wallPrefab) as MazeWall;
Renderer wallRender = wall.GetComponentsInChildren<Renderer>()[0];
wallRender.material.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
var distance = Mathf.Sqrt(Mathf.Pow((end.x - start.x), 2) + Mathf.Pow((end.y - start.y), 2));
wall.name = "Wall" + start.x + "," + start.y + "," + end.x + "," + end.y;
wall.transform.localScale = new Vector3(distance, 1f, 1f);
wall.transform.position = new Vector3((start.x - end.x) / 2, 0f, (start.y - end.y) / 2); }
I do not want to create a fixed size wall and then add more "unit walls" because i want to apply the same texture on one big pice. This is the expected result. I have tried to get the middle of the wall ass a point and then play with the "wall.transform.localRotation" but i think i do not use that correctly.
Thanks!
Answer by vssAndrei · May 05, 2018 at 02:08 PM
Found the answer!
private void CreateWall (Corner startCorner, Corner endCorner) { GameObject startObj = new GameObject(); GameObject endObj = new GameObject(); Vector3 start = new Vector3(startCorner.x, 1.5f, startCorner.y); Vector3 end = new Vector3(endCorner.x, 1.5f, endCorner.y); startObj.transform.localPosition = start; endObj.transform.localPosition = end; startObj.transform.LookAt(endObj.transform.position); endObj.transform.LookAt(startObj.transform.position); GameObject wall = GameObject.CreatePrimitive(PrimitiveType.Cube); // Set collor Renderer wallRender = wall.GetComponentsInChildren<Renderer>()[0]; wallRender.material.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)); // get distance float distance = Vector3.Distance(start, end); // Set possition wall.name = "Wall" + start.x + "," + start.y + "," + end.x + "," + end.y; wall.transform.localScale = new Vector3(0.1f, 3f, distance); wall.transform.position = startObj.transform.position + distance / 2 * startObj.transform.forward; wall.transform.rotation = startObj.transform.rotation; }
Your answer
Follow this Question
Related Questions
Problem with dynamic wall 1 Answer
Problem with dynamic wall 0 Answers
Programming dynamic barriers 1 Answer
2D Top-Down: Dynamically stretch wall to enclose rectangular area? 1 Answer
Dynamic wall movement 0 Answers