Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Nov 17, 2016 at 09:39 AM · rigidbodysnapsnapping

snap object to another object

how do I snap Cube1 to Cube2 when they are very close to each other? can it be used with rigidbody objects? grateful if anyone could help in c#

Comment
Add comment · Show 4
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 joshuauhler · Nov 17, 2016 at 06:23 PM 0
Share

What do you mean by snap?

avatar image $$anonymous$$ joshuauhler · Nov 18, 2016 at 01:18 AM 0
Share

eg: when Cube1 gets close to Cube2, Cube1 will automatically move towards Cube2 and stays nicely beside Cube2. hope it helps

avatar image alexanderameye · Nov 17, 2016 at 06:52 PM 0
Share

Do you want them to snap in edit-mode? If so take a look at this:

https://unity3d.com/learn/tutorials/topics/tips/snapping

avatar image $$anonymous$$ alexanderameye · Nov 18, 2016 at 01:13 AM 0
Share

no. I'm trying to snap objects in game and not in edit

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alexanderameye · Nov 18, 2016 at 08:30 AM

If these objects are simple cubes with all the same size, you first need to check whether or not the distance between the two objects is < some value. You can achieve this with Vector3.Distance.

https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

so something with this structure

 if(Vector3.Distance < 5.0f) snap

Then you want the objects to snap, this is a bit more tricky. With simple cubes, you could try this logic:

if the distance between the two cubes is small enough --> cube2.position.x = cube1.position.x + cube1.localscale.x --> cube2.position.y = cube1.position.y --> cube2.position.z = cube.position.z

OR

look at this tutorial

http://catlikecoding.com/unity/tutorials/frames-per-second/

where they also make some objects snap to one another

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 $$anonymous$$ · Nov 21, 2016 at 01:49 AM 0
Share

will try out the cube snapping logic out. regarding the tutorial, seems like they are using force to move and snap the balls together. I'm thinking, will snapping work using raycast?

avatar image
0

Answer by azizmohseny · Oct 03, 2018 at 04:48 PM

How to snapping 2D Ui to another 2D Ui when they ar very near to each other?alt text


snapping.gif (102.8 kB)
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

91 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 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 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 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

Need help with snapping Prefab assets to grid in game 2 Answers

Swipe to rotate an object and when it reach 30° snap it to 90° 0 Answers

Unity Snap settings. moving slightlty wrong 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Snapping using kinect v2 sdk 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