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 Tronton · Mar 08, 2014 at 07:47 PM · prefab-instanceprefabeper-poly

Minimum distance between cubes

I use this script:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     Ray ray;
     RaycastHit hit;
     public GameObject prefab;
     
     // 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.GetKey(KeyCode.Mouse0))
             {
                 GameObject obj=Instantiate(prefab,new Vector3(hit.point.x,hit.point.y,hit.point.z), Quaternion.identity) as GameObject;
                 
             }
             
         }
         
         
         
     }
 }

But, when I click, as well as create me 3 cubes at a time, it makes them all attached >.< alt text

How can I make sure that between a block and the other there is sufficient space to display Compleat well and cubes? Like minecraft, for inted D:

PS: Sorry for bad english.. but, all know google translate ^^"

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MakeCodeNow · Mar 09, 2014 at 04:20 AM

In a minecraft type game, there is a global, 3D grid of voxels, each the size of one cube. In that kind of world, you just find the center of the voxel nearest the ray intersection (that is not the cube you clicked on).

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 Tronton · Mar 10, 2014 at 02:06 PM 0
Share

Hello, the script used is old :C Now use I use this : #pragma strict

 var spawnAreas : Layer$$anonymous$$ask; //Layers where you can spawn objects, make sure to fill this out
 var destroyableObjects : Layer$$anonymous$$ask; //Objects that is ok to destroy, make sure to fill this out
 var spawnPrefab : GameObject; //The object you want to spawn, in case you didn't assign the script creates a primitive
 var grid : Vector3 = Vector3(1,1,1); //The grid you want to instantiate objects by
 var colliderSize : Vector3 = Vector3(1,1,1); //The collider size of the object
 var cam : Camera; //The camera you're using, in case you didn't assign one the script assigns the $$anonymous$$ain Camera
 private var range : float = 100.0; //Don't use infinity, use the camera's far clipping plane if you need to
 private var hit : RaycastHit;
  
 function Start () {
     cam = Camera.main;
 }
  
 function Update () {
     if (Input.Get$$anonymous$$ouseButtonDown(0))
         Build();
     if (Input.Get$$anonymous$$ouseButtonDown(1))
         Erase();
 }
  
 function Build () {
     if (HitBlock(spawnAreas)) {
        if (spawnPrefab==null) {
          //In case you didn't have a prefab we spawn a primitive
          spawnPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
          spawnPrefab.transform.position = Grid(hit.point);
        } else {
          //You have a prefab, let's spawn it
          var spawnObject = Instantiate(spawnPrefab, Grid(hit.point), Quaternion.identity);
        }
     }
 }
  
 function Erase () {
     if (HitBlock(destroyableObjects))
        Destroy(hit.transform.gameObject);
 }
  
 function HitBlock (mask : Layer$$anonymous$$ask) : boolean {
     var ray = cam.ScreenPointToRay (Input.mousePosition);
     return Physics.Raycast(ray, hit, range, mask);
 }
  
 function Grid (pos : Vector3) : Vector3 {
     return Vector3($$anonymous$$athf.Round(pos.x/grid.x)*grid.x-(colliderSize.x/2), $$anonymous$$athf.Round(pos.y/grid.y)*grid.y+(colliderSize.y/2), $$anonymous$$athf.Round(pos.z/grid.z)*grid.z-(colliderSize.z/2));
 }

But, thanks for the reply ^^

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

21 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

Related Questions

Problem with NGUI Removing and destroying Prefabs UIobjects in runTime 0 Answers

prefab Changes are not always applied in all scenes 0 Answers

Accesing variable from another class for instantiated gameobjects 0 Answers

instantiated prefab is invisible in game view but exists in the scene 0 Answers

GameObject variable points to instance instead of prefab when prefab gets instantiated 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