Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by unity_3G4TlFOPqD_EPA · May 22, 2018 at 01:08 PM · gridbuildingplacementblock

Minecraft like building?

I have a building mechanism that allows you to place blocks, it's all free and custom, though i want it to be in a grid, so when you place it, it'll be exactly the same to the grid.

i didn't try anything yet, but this is the script i've been using to place blocks:

 using UnityEngine;
 
 using System.Collections;
 
 public class PlaceBlock : MonoBehaviour
 {
 
     Ray ray;
     RaycastHit hit;
     public GameObject prefab;
     public GameObject prefab2;
     // Use this for initialization
     void Start()
     {
 
 
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             if (Input.GetKeyDown(KeyCode.X))
             {
                 GameObject obj = Instantiate(prefab, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
             }
             if (Input.GetKeyDown(KeyCode.Z))
             {
                 GameObject obj = Instantiate(prefab2, new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z), Quaternion.identity) as GameObject;
             }
         }
     }
 
 }


i have no idea how to acomplish it, i need help

this is how it looks so faralt text

screenshot-2.png (458.9 kB)
Comment
Add comment
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

2 Replies

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

Answer by MT369MT · May 22, 2018 at 06:01 PM

Hi, You must round your x, y and z values for example: GameObject obj = Instantiate(prefab2, new Vector3(Mathf.Round(hit.point.x - 0.5f) + 0.5f, Mathf.Round(hit.point.y - 0.5f) + 0.5f, Mathf.Round(hit.point.z - 0.5f) + 0.5f);

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 unity_3G4TlFOPqD_EPA · May 22, 2018 at 06:23 PM 0
Share

It works for the most part. though, with the "z" input, the blocks don't stack, while the "x" input does.. and oddly the blocks cannot be put next to eachother using eachother.. i'll put in a video for better details link text

Thanks for helping -Jessa

avatar image unity_3G4TlFOPqD_EPA · May 22, 2018 at 08:03 PM 0
Share

This is the current script, btw

 using UnityEngine;
 
 using System.Collections;
 
 public class PlaceBlock : $$anonymous$$onoBehaviour
 {
 
     Ray ray;
     RaycastHit hit;
     public GameObject prefab;
     public GameObject prefab2;
     // Use this for initialization
     void Start()
     {
 
 
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit))
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X))
             {
                 GameObject obj = Instantiate(prefab, new Vector3($$anonymous$$athf.Round(hit.point.x - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.y - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.z - 0.5f) + 0.5f), Quaternion.identity) as GameObject;
             }
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
             {
                 GameObject obj = Instantiate(prefab2, new Vector3($$anonymous$$athf.Round(hit.point.x - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.y - 0.5f) + 0.5f, $$anonymous$$athf.Round(hit.point.z - 0.5f) + 0.5f), Quaternion.identity) as GameObject;
             }
         }
     }
 
 }
avatar image MT369MT · May 23, 2018 at 05:28 AM 0
Share

Have the cubes some scripts? And have they also a rigidbody or other things?

avatar image unity_3G4TlFOPqD_EPA MT369MT · May 23, 2018 at 10:13 AM 0
Share

yess, they do. but the one with ridgid body works way better then the one without

avatar image unity_3G4TlFOPqD_EPA MT369MT · May 23, 2018 at 10:17 AM 0
Share

But the scripts and the ridgit body aren't the problem, as i also tried with normal cubes, they didn't work propperly either.

avatar image MT369MT · May 23, 2018 at 04:43 PM 0
Share

$$anonymous$$athf.Round round the .5 values to the nearest EVEN integer so 1.5 will be rounded to 2 and 2.5 also to 2. This is why sometimes the cubes go inside the other cubes. And obviously the raycast doesn’t know if he is touching on the left side or right side of the cube. You can try to do it with the normals of the colliders for example:

GameObject obj = Instantiate (prefab, hit.collider.transform.position + hit.normal, Quaternion.identity);

avatar image unity_3G4TlFOPqD_EPA MT369MT · May 23, 2018 at 07:48 PM 0
Share

I've got it working now, i just enlarged the cubes with 0.01 and now it works all nice and good. Thanks for your help!

avatar image
0

Answer by anthot4 · May 23, 2018 at 11:14 AM

@MT369MT is right, you need to mathf.clamp where the raycasting is hitting the ground. The reason the blocks don't stack is you need a "top collider" trigger on the top of the bricks to identify for the raycast that it is hitting the top of the brick. The game I am currently working on is similar to yours, I have got it working so that the bricks are stacked on top of each other and got the player able to rotate the bricks too.

Comment
Add comment · 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

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

84 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lego brick placement 0 Answers

Up to date Unity building placement code/tutorials? 1 Answer

How to make a grid and able to place buildings on? 0 Answers

Preview selected gameobject at hit.point 1 Answer

RTS Building Structures - How to have building follow my cursor until I click and place it on the grounds? 2 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