• 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 Fumbles · May 11, 2011 at 08:06 AM · gridclonevoxeladdminecraft

Place block script

I had put the break/place block script in my engine and when i right clicked for placing a block i got a huge gaping hole in the ground, breaking blocks with the left click works fine, Can i get a little help? This is the code..

 <p>var blockLayer : LayerMask = 1;
 var range : float = Mathf.Infinity;
 var hit : RaycastHit;</p>
 
 <p>function Update () {
     if (Input.GetMouseButtonDown(0))
         Build();
     if (Input.GetMouseButtonDown(1))
         Erase();
 }</p>
 
 <p>function Build() {
     if (HitBlock()) {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = hit.transform.position + hit.normal;
     }
 }</p>
 
 <p>function Erase() {
     if (HitBlock())
         Destroy(hit.transform.gameObject);
 }</p>
 
 <p>function HitBlock() : boolean {
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 }</p>
          

    
Comment
Add comment · Show 1
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 Fumbles · May 11, 2011 at 08:07 AM 0
Share

$$anonymous$$y bad, I've never put a code in a post before (:

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Dreamer · May 11, 2011 at 08:42 AM

You just select all the codes, and press the "101" button on top. I help you organize the codes first, then I take a look at it.

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

Comment
Add comment · Show 7 · 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 Dreamer · May 11, 2011 at 08:45 AM 0
Share

O$$anonymous$$, in this script. Left Click is to create a Cube object and Right click is to destroy the object you click on !

avatar image Fumbles · May 11, 2011 at 08:45 AM 0
Share

Oh, Haha I feel so silly now.. Okay tell me when your ready.

avatar image Fumbles · May 11, 2011 at 08:48 AM 0
Share

Well the left click was breaking blocks and the right click was breaking blocks but with a bigger size, Lol.. It may be the other scripts colliding with it or something.. I've been looking for a Break/Build script for awhile now.

avatar image ApplezGaming · Nov 01, 2012 at 11:23 PM 0
Share

i want to do the same sort of thing put to place and destroy where the mouse is not where the camera is looking could you please help change it to do that please?

avatar image henrylmoore · May 25, 2014 at 01:37 AM 0
Share

what type of format is the script

avatar image AlucardJay henrylmoore · May 25, 2014 at 03:09 AM 0
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Here at Unity Answers, Answer means Solution, not Response.

  • watch this video : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

  • read this page : http://answers.unity3d.com/page/faq.html

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Minecraft style grid and block placement? 2 Answers

Minecraft - Water Physics 1 Answer

Question about slideScrolling Voxels 0 Answers

Is MapMagic (asset) Voxel based? 0 Answers

Grid based water system 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges