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
0
Question by NochUnbekant · Jan 07, 2015 at 09:26 PM · minecraft

Problem at Minecraft like game

Hello I am programming a game like Minecraft. My problem is that I cant place blocks how i want it.

alt text

Here you can see that I am ponting on a block and want to place another block infront of it. The red line in the Scene view is the line were I am pointing at and the blue line is the line were the game wants to place the block.

My script for this is:

 using UnityEngine;
 using System.Collections;
 
 public class PlaceBlock2 : MonoBehaviour {
 
     public float range = 5; 
     public Transform prefab;
     RaycastHit hit;    
     
     // Use this for initialization
     void Start () {    
     }
     
     // Update is called once per frame
     void Update () {
         Ray ray = Camera.main.ScreenPointToRay (new Vector3(Screen.width*0.5f, Screen.height*0.5f, 0f));
 
         if (Physics.Raycast(ray, out hit,range)){
             // Pointing Ray
             Debug.DrawLine(transform.position,hit.point,Color.red);
 
             // Point to place
             Vector3 V = new Vector3(Mathf.Round(hit.point.x), Mathf.Round(hit.point.y+0.5f), Mathf.Round(hit.point.z));
 
             // Place Ray
             Debug.DrawLine(transform.position,V,Color.blue);
 
             // Place Block
             if(Input.GetMouseButtonDown(0)){
                 Instantiate(prefab,V,  Quaternion.identity);
             }    
 
         }
     }    
 }


Is my point to place wrong or were is my error.

unity minecraft problem.png (32.0 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Jan 07, 2015 at 09:46 PM

You could try using Mathf.Floor, or determine a point that is slightly inside the cube you are hitting by taking the hit.point and adding the camera's transform.forward * 0.01f to it, and then rounding this point instead. Besides that, take a look at the After playing Minecraft... thread, there are more sultions to your problem included.

Comment
Add comment · Show 3 · 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 NochUnbekant · Jan 08, 2015 at 01:51 PM 0
Share

adding the camera's transform.forward * 0.01f to it

I dont understand were I have to add it

avatar image Cherno · Jan 08, 2015 at 03:24 PM 0
Share

You have the hit.point. Now, you declare a new Vector3 and assign it the hit.point value plus the camera's transform.forward * 0.01f:

 Vector3 blockPos = hit.point + cameraObject.transform.forward * 0.01f;
 blockPos = new Vector3($$anonymous$$athf.FloorToInt(blockPos.x),$$anonymous$$athf.FloorToInt(blockPos.y),$$anonymous$$athf.FloorToInt(blockPos.z));


avatar image NochUnbekant · Jan 11, 2015 at 06:49 PM 0
Share

alt text

Again the same problem -.- I need something that round to x.5( if 1.3 than 1.5 and when 1.9 than 1.5 too)

unity minecraft problem 2.png (78.6 kB)
avatar image
0

Answer by jpthek9 · Jan 07, 2015 at 09:50 PM

You need 2 booleans to see which way to round to for the mine craft placement system.

 bool XPositive = hit.point.y > hit.transform.position.y ? true : false;
 bool XNegative = hit.point.x > hit.transform.position.x ? true : false;

If you need more help, just ask and I'll explain further.

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

Answer by gamer_bros_64 · Sep 11, 2020 at 10:20 AM

that kinda worked , but the blocks still place inside each other. plus they dont always work still. maybe try adding a line that says if a block in a piece of the grid is 1 then dont place there.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Most efficient method to create mesh from array 1 Answer

UV mapping 5 Answers

Using Meshes To Generate Cube Word? 2 Answers

Minecraft style pixels 1 Answer

Some questions about random world generation 1 Answer


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