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 /
avatar image
0
Question by maw0041 · Jul 11, 2019 at 02:47 PM · rigidbodymousecollidersjointsmouseclick

Mouse click ON cube to attach another cube to it

I don't know how to do this... I want a cube where I can click on one side of it and attach a cube to that one side.

I have rigidbody, I have colliders... I even put colliders on every single side of the cube (they are contained within an empty game object so that I can reference each by name).

Please help ...

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

Answer by ADiSiN · Jul 12, 2019 at 05:50 AM

Hi, @maw0041 !

This script should work for you, there is couple of implementations - currently it's finding center of cube and take it's rotation to attach accordingly, if you don't need it then remove function FindSideCenter() with assigning of transform.rotation and uncomment t_SelectedObject.position = hit.point + hit.normal.normalized f_CubeSize;*

 public Camera cam;
 public LayerMask layer;
 
 Transform t_SelectedObject;
 Transform t_AttachToObject;
 float f_CubeSize = 1f;
 
 // Update is called once per frame
 void Update()
 {
     /* If we press right mouse button  */
     if (Input.GetMouseButtonDown(1))
     {
         RaycastHit hit;
 
         /* Draw ray from mouse position to check if we hit anything with certain layer */
         if(Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, layer))
         {
             /* If we hit the same object then we de-select it */
             if (t_SelectedObject != null && t_SelectedObject == hit.transform)
                 t_SelectedObject = null;
             else
                 t_SelectedObject = hit.transform;
         }
     }
 
     /* If we press left mouse button */
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hit;
 
         /* Draw ray from mouse position to check if we hit anything with certain layer */
         if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, layer))
         {
             t_AttachToObject = hit.transform;
 
             if(t_SelectedObject != null)
             {
                 /* Check if we do not hit the same object that we have selected */
                 if(t_SelectedObject != hit.transform)
                 {
                     /* Assign position with offset so the cube center won't be at click point
                      * Keep in mind that if you change your cube size you should change f_CubeSize as well */
                     //t_SelectedObject.position = hit.point + hit.normal.normalized * f_CubeSize;
 
                     /* If you need to Rotate it accordingly to object that you attach */
                     t_SelectedObject.rotation = hit.transform.rotation;
 
                     /* Function if you want attach cube to center of hitted object side */
                     t_SelectedObject.position = FindSideCenter(hit.point);
 
                 }
             }
         }
     }
 }
 
 /* Function to find right side center and return it position to attach
  * If you don't need to attach it to center, then just remove this function */
 Vector3 FindSideCenter(Vector3 hitPosition)
 {
     Vector3[] sides = { t_AttachToObject.right, -t_AttachToObject.right, t_AttachToObject.forward, -t_AttachToObject.forward, t_AttachToObject.up, -t_AttachToObject.up };
 
     float minDistance = Vector3.Distance(t_AttachToObject.position + sides[0] * f_CubeSize, hitPosition);
     int sideIndex = 0;
 
     for(int i = 1; i < 6; i++)
     {
         float curDistance = Vector3.Distance(t_AttachToObject.position + sides[i] * f_CubeSize, hitPosition);
 
         if (curDistance < minDistance)
         {
             minDistance = curDistance;
             sideIndex = i;
         }
     }
 
     return t_AttachToObject.position + sides[sideIndex] * f_CubeSize;
 }

Put this script on any object, assign camera and layer that you want to interact with. The script works this way - by pressing your right mouse you select cube that you will move. By pressing left mouse we select cube to which we will attach. If you double click right mouse on the same cube it will de-select it. It has no visualization, so you can easily add color changin and other effects. If anything unclear - ask.

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

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

How to attach two colliders (i.e two or more game objects )? 1 Answer

Stop joint from jittering when target is on other side of collider 0 Answers

What is the best way for physics game? 0 Answers

Is there a way for colliders do work with Character joint without spasms? 0 Answers

How to properly convert mouse pos to world pos 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