well, I began learning from there and started to experiment trying to write a little bit of my own script I used a cube and changed it to the following dimensions x: 1, y: 0.5, z: 1 and made that a prefab after this I wrote the script below and I am not able to properly edit my script such that I can spawn the exact number of specified bricks on the Y axis, I think it is because of the line " y = y + 0.5f" inside the for loop for y but if I make it y++ there is an awkward gap between the blocks due to their shape.
So, could someone please suggest me how I could instantiate the brick wall keeping the shape of the brick.
sorry if this is not how problems are usually posted this is my first time sorry!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public GameObject brick;
public Transform Wall;
[Range(0,20)]
public int width;
[Range(0, 20)]
public int height;
void Start()
{
for (int x = 0; x < width; x++)
{
for (float y = a/2; y <= height; y = y + 0.5f)
{
Instantiate(brick, new Vector3(x, y, 0), Quaternion.identity, Wall);
}
}
}
}
No, I didn’t mean I wasn’t able to spawn a higher wall I meant that when I give it a height of 5 only 4 blocks are instantiated on the y-axis whereas, when I give it a height of 6 it gives me a wall 6 blocks of height.
and the missing part of code that is present is about the variable ’ a ’ which is just a variable to store the value of the y component of the transform of the brick, which for some reason did not seem to get copied sorry!!
try to calculate new position to spawn without using float x,y in loop, something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public GameObject brick;
public Transform Wall;
[Range(0,20)]
public int width;
[Range(0, 20)]
public int height;
void Start()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector3 place = new Vector3 (x, y/2f, 0);
Instantiate(brick, place, Quaternion.identity, Wall);
}
}
}
}
Thank You for your quick reply but doesn’t your code just ask the whole wall to shift down? instead of each individual block? what I mean I something simple like in your code you basically ask each block to shift half of the coordinate down right basically its like telling the whole wall to shift a little down?
sorry, I meant: use int number of bricks for loops, and then calculate the place position.
the script depended on your brick can be:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public GameObject brick;
public Transform Wall;
[Range(0,20)]
public int width;
[Range(0, 20)]
public int height;
void Start()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector3 place = new Vector3 (Wall.position.x + brick.transform.localScale.x * x,
Wall.position.y + brick.transform.localScale.y * y, 0);
Instantiate(brick, place, Quaternion.identity, Wall);
}
}
}
}