- Home /
Help with a Cubeland-type script.
I finally got the script, I just want to thank jesse anders for giving me the unity documentation to help me figure the script out sorry for posting a lot. Anyways to the script when I put the script on my cube prefab and try to place the cube, it just goes crazy. What I mean is it will stack 20 cubes high from the ground. I don't want that, I want to stack one cube at a time. When I place a cube manually, I should be able to stack manually and stacking nice and neat.
This is not in editor mode this is in play mode and its in first person shooter. I probably confused you guys sorry about that hahahaha. If you guys still don't get what I'm trying, to say watch this video and you will see the player can stack the cubes nice and neat and the player can build like a castle or a house - thats what I want for my game. and stacking beside each other. Cubelands video
Heres the script:
function Start()
{
for(i=0; i<20; i++)
{
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = transform.position + Vector3(0, i, 0);
}
}
Of course it stacks 20 cubes high from the ground. That's exactly what the script you posted says to do. How is that "going crazy"?
Answer by Statement · Mar 31, 2011 at 01:39 PM
- Try online
- Download package
- Source code:
Put this on an empty game object to generate floor:
for (var y = -4; y < 5; ++y)
for (var x = -4; x < 5; ++x) {
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = transform.position + Vector3(x, 0, y);
}
Put this on your camera to build or erase cubes:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
}
}
function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
Or a more compact version that does the same as the two above, but creates ground 3 units below player:
var range : float = Mathf.Infinity;
var hit : RaycastHit;
for (var y = -4; y < 5; ++y)
for (var x = -4; x < 5; ++x) {
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = transform.position + Vector3(x, -3, y);
}
function Update () {
if (Input.GetMouseButtonDown(0) && Hit()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
}
if (Input.GetMouseButtonDown(1) && Hit())
Destroy(hit.transform.gameObject);
}
function Hit() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, 1);
}
Set your players layer to Ignore Raycast.
Thank you soo much dude you dont know how much this means to me for the game your a lightsaver. i only got one more question if i wanna make the floor bigger i just change the y < 5; to lets say 500 by 500. i wanna thank you one more time because i love you no homo hahahahaha.
@statment thanks man this is awesome i appreciate it i really do. i got one more question though if i wanna make the floor bigger do i just change the y < 5; to lets say y < 10;
500 by 500 become 250000 blocks. Unity can't handle that many colliders and draw calls. But if you want to lockup/crash your computer go ahead. You need to try some stuff out by yourself. Edit something in the code and see what happens. Learn by doing.
How would I stop it from being able to destroy the player...
Answer by demize2010 · Mar 31, 2011 at 10:14 AM
Hey man, can you be clearer in what you are looking for? Your question is not very well structured, try to clearly outline the characteristics of your block creation and interaction in bullet points to convey the idea.
The problem your encountering is due to the fact that your instancing code is in the Start() function. By placing this code in start it will instance all 20 blocks one after another (pretty much instantly) when the level starts. If these blocks have rigidbodies they'll fall about all over the place in the physics engine.
You need to take the instancing syntax and tie it to a function called by an input event. I'm not sure what your final goal is, if your shooting for something like Minecraft I suspect your going to need an array that keeps track of vector3 positions where objects exist or some alternative system of tracking block positioning vis a vis other blocks.
Yea like $$anonymous$$ecraft you know how you like build a house or something thats what im trying to do i got the inputs set up already
Your answer
![](https://koobas.hobune.stream/wayback/20220613115908im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Minecraft world generation in JavaScript 0 Answers
World Generation with Interesting Terrain 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Pause Menu Background 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers