• 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 thornekey · Jan 16, 2014 at 01:29 AM · spawnsnap

Snap Objects when Spawning

Hey all, im having trouble snapping my objects together when they hit. like, im tryna make it so that when i spawn one near another, it doesnt go into it but rather next to or on.. anyone got a good solution?

 var cubeobj : Transform;
 
 function Update () {
 
 var mousex = Input.mousePosition.x;
 var mousey = Input.mousePosition.y;
 var mousez = Input.mousePosition.z;
 var ray = camera.main.ScreenPointToRay(Vector3(mousex,mousey,mousez)); 
 var hit : RaycastHit;
 
     if(Physics.Raycast (ray, hit, 200)) {
     } 
 
     if ( Input.GetButtonDown("Fire1") ){
         var cube = Instantiate(cubeobj, hit.point, Quaternion.identity);
     }
 }
Comment
Add comment · Show 8
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 thornekey · Jan 16, 2014 at 07:06 AM 0
Share

bumpedy bump anyone

avatar image getyour411 · Jan 16, 2014 at 08:08 AM 0
Share

Not related to the answer, but why split the xy(Z?) of mouse.pos and then rebuild it as a Vector3 (which it already was) into the input of ScreenPointToRay? Did you try:

 ScreenPointToRay(Input.mousePosition)

Do your cubes have a collider/trigger on them?

avatar image thornekey · Jan 16, 2014 at 08:57 AM 0
Share

the cubes have a box collider

avatar image nesis · Jan 16, 2014 at 09:02 AM 0
Share

If you're working with Unity's cubes, you'll need to offset them from the hit.point by adding hit.normal * (size of the cube).

With some code refactoring to show you a more efficient way to do what you want, here's an example:

 var cubeobj : Transform;
 function Update() {
     if ( Input.GetButtonDown("Fire1") ){ //this if statement is quicker to check, so do it first to avoid doing a raycast even when the user hasn't clicked
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition); //as getyour411 suggested
         var hit : RaycastHit;
         if(Physics.Raycast (ray, hit, 200)) {
             var spawnPoint : Vector3 = hit.point + hit.normal * 0.5f;
             //the above line assumes your cube is 1 unit wide, 
             //and has its origin at its centre (hence 
             //offsetting it by half its width)

             //spawn the cube
             var cube = Instantiate(cubeobj, spawnPoint, Quaternion.identity);

             //note that how you offset objects from one another
             //will need to change depending on what shape your object is
         }
     }
 }
avatar image thornekey · Jan 16, 2014 at 09:14 AM 0
Share

but then i get this error: NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) InputHandler.Update () (at Assets/InputHandler.cs:20)

avatar image nesis · Jan 16, 2014 at 09:25 AM 0
Share

The error there says it's in a .cs file, which is C#. The code you posted was Javascript, so I wrote my answer in Javascript. It won't work in C#. What language were you wanting to use?

avatar image thornekey · Jan 16, 2014 at 11:13 AM 0
Share

sorry. lack of sleep - i created the wrong file haha

avatar image thornekey · Jan 16, 2014 at 11:17 AM 0
Share

iive got rid of that error now this has occured though..`NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) Spawn.Update () (at Assets/Spawn.js:4) `

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

onTrigger spawn object C# 3 Answers

Spawning Statement Help 0 Answers

Spawning objects help 2 Answers

How to make things spawn on the ground 1 Answer

Trigger...Spawn...Destroy 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