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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Infektado · May 07, 2014 at 06:49 PM · assemblysnapsnappingpuzzle

How to snap objects in game

I been looking for some script for assembly objects in a puzzle 3d game, my original idea is make this parent emptys to the big objects & add the snap script for this empty, with this build a bigger object. this is an example what I want to do, but without th leap motion & the bike.

http://www.youtube.com/watch?v=MKz7DdEH9mQ

Is there any example for make this kind of snapping? or do you have any idea for snapping via scripting? Thanks!!

Comment
Add comment · Show 3
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 robertbu · May 07, 2014 at 08:15 PM 0
Share

The actual snapping in the video is pretty simple, but there are number of issues leading up to the snap. For example, without leap motion, how do you plan to deal with the 'z' axis? Or is this really a 2D game using 3D items? Are items only added to a whole like in the video or do individual items also snap together? Drag and drop is notably harder than the snapping code. Do you have drag and drop working?

avatar image Infektado · May 08, 2014 at 06:57 PM 0
Share

thanks for replay, in fact I want to make this in a tablet devices for snap blocks, this blocks will be in front and the main figure will be in background, this game will be in 3d for assembly diferents figures dragging & drop this pices. actually drag & drop I found more examples and documentation to solve it. Thanks again & I'll wait for a tip or example.

avatar image robertbu · May 08, 2014 at 09:21 PM 0
Share

If this is 3D, how do you plan to deal with depth? Fingers/screen are 2D. I assume you are using a perspective camera?

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · May 08, 2014 at 10:11 PM

Here is and example script. To make it work you need to pair each part with game object. It may an empty game object. When you get close to that part (as viewed from a perspective camera), it will change color. To figure out close, the code projects the 3D position of the part and the partner game object onto a viewport and uses Viewport coordinates to do the calculation. If you drop the part while it is close, it moves and rotates into position as defined by its 'partner' object. Note that the color change depends on have a shader that supports material.color. Also it there is no drag and drop here and therefore must be done in a separate script.

 #pragma strict
 
 var partnerName = "Hub";
 var closeVPDist = 0.05;
 var moveSpeed = 40.0;
 var rotateSpeed = 90.0;
 
 var closeColor = Color(1,0,0);
 
 private var dist = Mathf.Infinity;
 private var normalColor : Color;
 private var partnerGO : GameObject;
 
 function Start() {
     normalColor = renderer.material.color;
     partnerGO = GameObject.Find(partnerName);
 }
 
 function OnMouseDrag() {
     var partnerPos = Camera.main.WorldToViewportPoint(partnerGO.transform.position);
     var myPos = Camera.main.WorldToViewportPoint(transform.position);
     dist = Vector2.Distance(partnerPos, myPos);
     renderer.material.color = (dist < closeVPDist) ? closeColor : normalColor;
 }
 
 function OnMouseUp() {
     if (dist < closeVPDist) {
         transform.parent = partnerGO.transform;
         InstallPart();
     }
 }
 
 function InstallPart() {
     while (transform.localPosition != Vector3.zero || transform.localRotation != Quaternion.identity) {
         transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.zero, Time.deltaTime * moveSpeed);
         transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, Time.deltaTime * rotateSpeed);
         yield;
     }
 }
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 Infektado · May 09, 2014 at 03:37 PM 0
Share

Ok, I'll test it rigth now!!! Thanks again, soon I'll coment the results.

avatar image maromero · Sep 24, 2016 at 11:37 PM 0
Share

Hi @robertbu, for some unknown reason I haven't been able to work your "example script" for HTC Vive controls. Do you know some package in Unity Asset Store that includes this function for Vive controls?

Very grateful in advance for your advice.

avatar image
0

Answer by DJBooooooom · Feb 17, 2019 at 08:46 AM

could you translate it into c# please?

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

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

Related Questions

How do I snap/assemble two or more oddly shaped objects together? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to drag gameobject (only x y) snapped to a grid? 2 Answers

Snapping Objects Together in 2D Scene 3 Answers

Character snap to camera direction 0 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