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
1
Question by Subtle · Sep 29, 2013 at 02:53 PM · javascriptrotate objectsnap

Snap a object position and direction

Is there a way to snap a object position and direction?

For example, there are two sticks. When they are in a range, they are snapped position and direction. Finally they become like one pole.

Thanks.

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 clunk47 · Sep 29, 2013 at 05:26 PM

I get an array of all transforms on awake, then create a generic list of specific transforms with the same name, then duplicate that list, so you can compare the distance between each object. In this C# example, once poles are in range of one another, they move toward one another until they are snapped into one another's position. For testing I created a few cubes and scaled them to look like "sticks" or "poles", then I created an empty gameObject for each stick, then set the empty on TOP of half the poles, and on BOTTOM of the other half, then made the empty the parent of it's stick, and named each parent empty "pole" to work with my example. You'll need to do this for the top on some and bottom on others so the pivot points snap together. This is a really small and simple example I came up with quickly, just to get you started. Break down the code and look up each thing that you may not understand in the Unity Script Reference.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     float range = 1.0f;
     float moveSpeed = 0.25f;
     Object[] transforms;
     List<Transform> poles;
     List<Transform> polesCompare;
     
     void Awake()
     {
         poles = new List<Transform>();
         polesCompare = new List<Transform>();
         transforms = Object.FindObjectsOfType(typeof(Transform));
         foreach(Transform t in transforms)
         {
             if(t.gameObject.name == "pole")
             {
                 poles.Add (t);
                 polesCompare.Add (t);
             }
         }
     }
     
     void Update()
     {
         if(poles.Count > 0)
         {
             foreach(Transform p in poles)
             {
                 foreach(Transform pc in polesCompare)
                 {
                     float dist = Vector3.Distance(p.position, pc.position);
                     if(dist <= range)
                         p.position = pc.position;
                 }
             }
         }
     }
 }
 
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 maromero · Sep 24, 2016 at 11:56 PM 0
Share

Hi @clunk47, do you know some package in Unity Asset Store that includes this "Snap function" for HTC Vive controls?

Very grateful in advance for your advice.

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

17 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

Related Questions

Rotation Jerks when Copying From Different Object 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Rotating GameObject with transform.Rotate issue! 1 Answer

Object deformation on activat 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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