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 DGKN · Feb 23, 2015 at 04:02 PM · c#rigidbodyraycastfixedjoint

Attach rigidbodies using a fixed joint on mouseclick

Hi,

What is the proper way to attach two or more rigidbodies together when the player clicks on one of the objects ?

Right now, I can only connect 2 objects together. When I try to connect a third one to the others that are connected, it doesn't work. What can I do to correct that please ? Thanks!

Here's what I've done so far :

 void Update()
     {
         if(Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3((Screen.width / 2), (Screen.height / 2), 0)), out hit, maxBuildDist))
         {
             if(hit.transform.tag == "piece")
             {
                 if(Input.GetMouseButtonDown(1))
                     touched = true;
             }
         }
     }
     
     void OnCollisionStay(Collision c) 
     { 
         if(!attached)
         {
             if(touched)
             {
                 var joint = gameObject.AddComponent<FixedJoint>(); 
                 joint.connectedBody = c.rigidbody; 
                 joint.enableCollision = true;
                 attached = true;
                 touched = false;
             }
         }
     }

Comment
Add comment · Show 9
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 skylem · Feb 23, 2015 at 04:23 PM 1
Share

have u considered placing the joint upon the object of collision?

  c.gameObject.AddComponent<FixedJoint>(); 
 
 then
 
 c.GetComponent<FixedJoint>().connectedBody = rigibody;

and if i were doing this i would add all the connected objects to List; to manage the objects that i have connected.

avatar image DGKN · Feb 23, 2015 at 05:59 PM 0
Share

Yes, I've tried doing that but it still doesn't work. I can connect two rigidbodies together but not those two to a third one. Thanks anyway

avatar image skylem · Feb 24, 2015 at 02:11 AM 0
Share

you'll probably want to setup something to assign the object you click to a temporary transform, say for instance On$$anonymous$$ouseDown fill this transform then if this transform is not null and we On$$anonymous$$ouseDown again on a different object connect the two then empty this transform if you need more details just ask.

avatar image skylem · Feb 24, 2015 at 02:48 AM 0
Share

Heres an example of what i mentioned before, its in c# appologies i don't have time to convert it right now, perhaps it can provide some incite.

 public Transform connecter;
 public Transform connectee;

         RaycastHit hit = new RaycastHit ();
         Debug.DrawRay (transform.position, transform.forward * 10);
         Physics.Raycast(transform.position, transform.forward * 10, out hit);
 
         if(Input.GetButtonDown("Fire1") && connecter == null) {
             if(hit.transform != null && hit.transform.tag == "Click") {
                 connecter = hit.transform;
             }
         }
         else if(Input.GetButtonDown("Fire1") && connecter != null && connectee == null) {
             if(hit.transform != null && hit.transform.tag == "Click") {
                 connectee = hit.transform;
             }
         }
         if(connecter && connectee != null) {
             connecter.gameObject.AddComponent<FixedJoint>();
             connecter.GetComponent<FixedJoint>().connectedBody = connectee.rigidbody;
             connecter = null;
             connectee = null;
         }
 
         if(Input.GetButtonDown("Fire2") && connecter != null) {
                 connecter = null;
                 connectee = null;
         }
     }
avatar image DGKN · Feb 24, 2015 at 03:02 PM 0
Share

Thank you for your help skylem, I've tried your code but I don't know how to make it work on my current setup.

I've just replaced hit.transform.tag == "piece" to hit.transform == transform and my code seems to work now ? Strange.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

How to make a ball stay in the air longer when force is added? 1 Answer

How to aim a ball with cross hair? 2 Answers

Seeking help or Tutorials for Raycast/movement script. 1 Answer

How do I add force to rigidbody when raycast hits it. 1 Answer

RigidBody character wont swing? 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