• 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 $$anonymous$$ · Jun 23, 2015 at 11:18 PM · prefabs

Instantiate a cube prefab and snap it to another (c#)..

Hi everyone, I'm creating a 3D platformer and i'm stuck with one of my scripts. In my game the player can remove and place objects (Cubes actually). So far I managed to make the player remove the right cubes but I just don't see how to make the player to place a cube next to another one (snap it) when clicking on the face (something like minecraft snapping).

I searched and found a lot of things regarding how to build some minecraft gameplay but I never managed to make it work in my game.

There are 3 types of cube in my game :

Green: They are "Level cube" and cannot be removed but we can snap other cubes to them

Red : They are "Weak cubes" the can be removed and we can snap cubes to them

Blue : They are the "Player cubes" they can be placed and removed

For now i'm abble to remove red and blue cubes, but when i try to place a blue cube next to another one it goes right at the place of the cube i clicked on, i'm not sure if I explain that clearly what it does is that : alt text

The 2 cubes are in the same place.(it does the same thing with de green cubes)

I tried to use a grid and layermasks (as found in one of the subjects about minecraft stuff in unityAnswer) but it didn't worked (the cube was instantiated far from where i clicked)

I'm quite lost here if someone could help me that would be awesome !

Here is my code so far :

     public float Range = 10.0f;
     public GameObject Cube;
 
     
     void Update() 
     {
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hitPoint;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
 
             if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity))
             {
                 if(hitPoint.collider.tag == "PlayerCube" && (hitPoint.distance <= Range))
                 {
                     Destroy(hitPoint.collider.gameObject);
                     Debug.Log("Hit/Removed PlayerCube"); 
                 }
         
                     
                 if(hitPoint.collider.tag == "LevelCube" && (hitPoint.distance <= Range))
                 {
                     Debug.Log("Hit/Can't Remove LevelCube"); 
                 }
 
                     
                 if(hitPoint.collider.tag == "WeakCube" && (hitPoint.distance <= Range))
                 {
                     Destroy(hitPoint.collider.gameObject);
                     Debug.Log("Hit/Removed WeakCube"); 
                     
                 }
             }
                 
             else
             {
                 Debug.Log ("No collider hit"); 
             }
         }
 
 
 
         if (Input.GetMouseButtonDown(0))
         {
             
             RaycastHit hitPoint;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
 
             if(Physics.Raycast(ray, out hitPoint, Mathf.Infinity))
             {
                 if(hitPoint.distance <= Range)
                 {    
                     Instantiate(Cube, hitPoint.transform.position, Quaternion.identity);
                     Debug.Log("Placed a Cube"); 
                 }
 
             } 
         }
     }
 
 


Thanks for your help :) And sorry for my bad english.

cap2.jpg (41.2 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

1 Reply

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

Answer by TrainWrek · Jun 23, 2015 at 11:41 PM

Here is an example of placing a cube on the surface of another default cube.

 public class PlaceCube : MonoBehaviour {
     public float Range = 10f;
     public GameObject Cube;
 
     private void Update() {
         if (Input.GetMouseButtonDown(0)) {
             RaycastHit hitPoint;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
             
             if (Physics.Raycast(ray, out hitPoint, Range)) {
                     Instantiate(Cube, hitPoint.normal + hitPoint.transform.position, hitPoint.transform.rotation);
                     Debug.Log("Placed a Cube"); 
             } 
         }
     }
 }


Comment
Add comment · Show 2 · 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 $$anonymous$$ · Jun 24, 2015 at 02:05 PM 0
Share

Well I can't thank you enough ,you solved my problem, I just had to put x2 to the normal because my cubes are 2x2x2

Instantiate(Cube, (hitPoint.normal*2) + hitPoint.transform.position, hitPoint.transform.rotation);

It works perfectly, again, thanks :)

avatar image unity_mAOQG6kcIcXzjA · Aug 12, 2019 at 03:38 AM 0
Share

Works like a charm. Thank you!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How would I go about storing spawned prefabs to acces them ? 3 Answers

What files are responsible for script components on prefabs? 2 Answers

When creating a 2D array as a grid of cubes they are all the same colour,My array of gameObjects are all the same colour, even when I set them differently 2 Answers

Breakable objects are still broken after level reset 2 Answers

Unity crashes when try to load prefab with LWRP from Asset bundles 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