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 /
This question was closed Jul 24, 2013 at 07:43 PM by davidflynn2 for the following reason:

I found a work around for the problem that allowed me to get it working.

avatar image
0
Question by davidflynn2 · Jul 24, 2013 at 06:30 PM · c#spawningoffset

Spawning off size blocks

I have the following scrip allowing me to spawn blocks in my scene when I do it on a standard size cube they stack just fine but if I go to any size other size then that they tend to not stack correctly. Most of the time they stack inside the one you are trying to spawn it on top or side of.

 using UnityEngine;
 using System.Collections;
 
 public class CubeBuilder : MonoBehaviour 
 {
 
     float range = Mathf.Infinity;
     public GameObject currentBlock;
     public Transform curBlock;
     RaycastHit hit;
     public GameObject piece;
     public GameObject piece1;
     private Transform child;
     Vector3 hitPoint;
     private Rect checkRect = new Rect(0,0,160,160);
     public Ray ray;
     private GameObject[] blocks;
 
     void Start ()
     {    
         blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
     }
 
 
     void  Build ()
     {
          ray= Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         
         if (Physics.Raycast (ray,out hit, 1000))
         {hitPoint = hit.transform.position + hit.normal;
             Instantiate(currentBlock, hitPoint, curBlock.rotation);
         }
     }
 
     void  Erase ()
     {
         ray= Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         
         if(blocks.Length> 50)
         {
             if (Physics.Raycast (ray, out hit, 1000))
             {
                 piece1 = hit.transform.gameObject;
                 piece = piece1.GetComponent<ParticleHolder>().ruble;
                 
                 hitPoint = hit.transform.position + hit.normal;
                 Instantiate(piece, hitPoint,curBlock.rotation);
                 Destroy(hit.transform.gameObject);
                  
             }
         }
     
     }
 
     void  Update ()
 
     {
         blocks = GameObject.FindGameObjectsWithTag("WorldBlocks");
         if (!checkRect.Contains(Input.mousePosition))
         {
             if (Input.GetMouseButtonDown(0))
                 Build();
             if (Input.GetMouseButtonDown(1))
                 Erase();
         }
     }
 
 }
 
 
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

  • Sort: 
avatar image
0

Answer by robertbu · Jul 24, 2013 at 06:38 PM

Your problem is on line 31:

 hitPoint = hit.transform.position + hit.normal;

The hit.normal has unit length (length of 1), so your hitPoint is exactly one unit away from the center of the object hit. To fix your code, you need to scale hit.normal by the size of your blocks. Assuming you've uniformly scaled on all three axes to to 'scale' size:

 hitPoint = hit.transform.position + hit.normal * scale;

If the blocks are not uniformly scaled, then you are going to have to figure out what direction the hit.normal is facing and use the scale for the appropriate axis.

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 davidflynn2 · Jul 24, 2013 at 06:52 PM 0
Share

Ok my blocks are not uniform because some of them are a lot thinner in one direction then the other.

avatar image robertbu · Jul 24, 2013 at 07:09 PM 0
Share

Then you are going to have to figure out which axis of the block was hit. Do you have blocks of different sizes, or are they all sized the same (i.e. I know the they are not uniformly scaled, but is the length width and height the same for all blocks)? Do the blocks align with the world axes or are they rotated? Vector3.Dot() is what I'd use to detect the axis, but how will depend on the answers to these questions.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Multiple Cars not working 1 Answer

Setting Spawn Rotation correct 1 Answer

Spawning A Grid Of Cubes 2 Answers

Object Spawning Too Low 0 Answers

Distribute terrain in zones 3 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