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 /
avatar image
0
Question by ZXYNINE · Dec 06, 2014 at 03:34 PM · drag-and-dropsnap

Snap to place

i am trying to make it so that if an object that im dragging around gets close to any empty Gameobject with the name "TopSnap" then it will move to the top of the object and if it gets close to anything names "BottomSnap" then it will move to the bottom. I will have a few cylinders with 2 empty game objects on it one will be the top snap and the other is the bottom snap.

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
3
Best Answer

Answer by EvilTak · Dec 06, 2014 at 04:47 PM

If you are using rigidbodies, you can create triggers in your empty gameobjects to represent the area in which the object will snap. Then add a script to them with the OnTriggerEnter function, in which make the position of the object which enters the trigger same as the empty gameobject in which it entered. Or if you are not using rigidbodies, each update check the sqrdistance between each object and the topsnap or bottomsnap gameobjects. If it is less than a certain value, then snap the object. Personally I prefer the first method, with kinetic rigidbodies and using rigidbody.MovePosition(). It is easier to implement and involves just half a dozen lines of code.

EDIT: Steps for first method:

  1. Add any tag to your cylinders or gameObjects which you want to snap

  2. Add a box collider or sphere collider to both TopSnap and BottomSnap objects, resize it to depict the area in which the object, on entering will be snapped. This can be any size you like.

  3. Add rigidbodies to all gameObjects you want to snap and make sure they have a collider (collider should be on same transform as rigidbody).

  4. void OnTriggerEnter(Collider other) { if(other.gameObject.CompareTag("the_tag_which_you_kept_earlier") { other.rigidbody.MovePosition(transform.position); } }

  5. Attach the script to the TopSnap and BottomSnap gameObjects

  6. Done!

Comment
Add comment · Show 8 · 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 ZXYNINE · Dec 06, 2014 at 08:23 PM 0
Share

Hay thanks for the reply but im a but rusty in unity do you think you could help?

avatar image EvilTak · Dec 07, 2014 at 03:17 AM 0
Share

Edited my answer to include steps. If it helped you please accept it.

avatar image ZXYNINE · Dec 07, 2014 at 03:42 AM 0
Share

Thanks man!

avatar image ZXYNINE · Dec 07, 2014 at 04:02 PM 0
Share

Alright so i tried but nothing happens when they get close this is what i have done so far...

i made a tag and put it on my two cylinders then i made two empty game objects and named one topsnap and the other bottom snap then put both as a child of the cylinder then i added a sphere collider to both top and bottom snaps and i added a ridgid body on the cylinders along with a collider then i made the script and added it to top and bottom snaps then i tried and its not working so what have i done wrong?

avatar image EvilTak · Dec 07, 2014 at 04:06 PM 0
Share

Why have you put topsnap and bottomsnap as the children of a cylinder? Btw, the cylinders will have to enter the sphere collider (should have isTrigger marked as true) of topsnap or bottomsnap to get snapped.

Show more comments

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

26 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

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Rotation Snapping is adding harmful floats! 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Building in Unity 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