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 Hjalte · Mar 27, 2015 at 04:11 PM · movementrigidbodymousehingejointchain

Hinge joints disconnect when pulled too far.

I have this chain that consists of a bunch of hingejoints connected together. I want to control this chain with the mouse but when the mouse gets too far away the chain starts to disconnect and act weird. I want the chain to stop extending when its been pulled to its max length but still follow the mouse.

Heres some code:

             mouseX = Input.mousePosition.x;
             mouseY = Input.mousePosition.y;
             Vector2 mousePos = cam.GetComponent<Camera>().ScreenToWorldPoint(new Vector2(mouseX, mouseY));
             rb.MovePosition(mousePos);

alt text

Sorry for bad explanation.

joints-disconnecting.png (77.7 kB)
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 hexagonius · Mar 27, 2015 at 04:25 PM 1
Share

From the length of the segments and their distances you should be able to calculate the maximum length of the chain. I assume it's connected on one end and you're moving the other end. What you could do is restrict the mouse position to the maximum length of the chain from it's anchor.

If distance > maxLength, calculate direction Vector from anchor to mouse, clamp it's magnitude and add it to the anchor position. That's your new mouse position

avatar image Hjalte · Mar 27, 2015 at 08:06 PM 0
Share

Okay I came up with some restriction but now the cahin seems to be... sagging? if i hold the mouse still the last link will start falling. Anyway to avoid this? alt text

sagging-chain.png (169.5 kB)
avatar image NoseKills · Mar 27, 2015 at 09:16 PM 1
Share

The wheight of the chain probably makes it stretch and become a bit longer than its calculated length. You might just have to add a few percent to the length of the calculated clamped mousePosition

avatar image Hjalte · Mar 27, 2015 at 11:15 PM 0
Share

Okay I didnt actually use the clamp thing but I put the gravity scale to 0 on the object and increased the mass. Im gonna put my script here if anyone needs it.

     GameObject cam;
     public Transform center;
     public float radius;
     float dist;
     Vector2 mousePos;
     Vector2 screen$$anonymous$$ouse;
     Vector2 mouseOffset;
 
     void Start()
     {
         cam = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
     }
 
     void Update()
     {
         mousePos = Input.mousePosition;
         screen$$anonymous$$ouse = cam.GetComponent<Camera>().ScreenToWorldPoint(new Vector2(mousePos.x, mousePos.y));
         mouseOffset = new Vector2(screen$$anonymous$$ouse.x - center.position.x, screen$$anonymous$$ouse.y - center.position.y);
         transform.position = new Vector2(screen$$anonymous$$ouse.x, screen$$anonymous$$ouse.y);
 
         dist = Vector2.Distance(new Vector2(transform.position.x, transform.position.y), new Vector2(center.position.x, center.position.y));
         if(dist > radius){
             Vector2 norm = mouseOffset.normalized;
             transform.position = new Vector2(norm.x * radius + center.position.x,
                                              norm.y * radius + center.position.y);
         }
     }

This is still test code btww so I know theres some stuff I could probably do differently

Thanks for the help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player slows down when hitting an incline 0 Answers

Hinge Joints Twisting around Wrong Axis 0 Answers

Roll-A-Ball Movement (Without Endless Run) [C#] 2 Answers

How do you move a rigidbody by a vector3 relative to itself? 1 Answer

How to make rigidbody bounce off the mouse? (2D) 2 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