Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by user-10911 (yahoo) · Mar 31, 2011 at 04:58 AM · javascriptminecraft

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);
    }
}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiloblargh · Feb 22, 2013 at 11:46 PM 0
Share

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"?

avatar image RadhaUnity3d · Nov 15, 2013 at 07:57 AM 0
Share

The video is not playing.............

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-10911 (yahoo) · Mar 31, 2011 at 11:28 PM 0
Share

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.

avatar image user-10911 (yahoo) · Mar 31, 2011 at 11:47 PM 0
Share

@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;

avatar image Statement · Apr 01, 2011 at 11:11 AM 0
Share

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.

avatar image Toggy11 · Jan 10, 2014 at 08:10 PM 0
Share

How would I stop it from being able to destroy the player...

avatar image J-R-Wood · Feb 24, 2014 at 12:01 AM 1
Share

@Statement that download was beautiful, and simple.

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image user-10911 (yahoo) · Mar 31, 2011 at 10:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges