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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DeeCeptor · Dec 25, 2014 at 06:48 PM · 2dboxcollider2dhingejoint

Connecting HingeJoint2D to clicked upon spot

I'm creating a 2D game where the user can click and drag objects around. This is done by connecting a 'Hand' object to the object the user is current clicking on with a HingeJoint2D. Right now it simply connects the HingeJoint2D to the center of the clicked upon object.

I want to anchor the Hingejoint2D to the exact spot on the rigidbody (not the center) that the user is clicking on. This must involve transforming the clicked upon point to coordinates relative to the clicked upon rigidbody, but I've had no success using methods like transform's TransformPoint.

Any help would be appreciated.

This is the code I have attached to the object that the user will be dragging around:

 void OnMouseDown() {        
     // Create a hinge joint between the hand and this object so object will slowly rotate
     // around the hinge point
     this.gameObject.AddComponent("HingeJoint2D");
     connectingHinge = this.GetComponent<HingeJoint2D>();
     connectingHinge.connectedBody = handRigidbody;
     
     // Set anchor to offset of current hand position relative to the object.
     //connectingHinge.anchor = new Vector2(offset.x, offset.y);
 }

Image of gameplay: alt text

The gray box is where the mouse cursor is. The gun is an object that the user is dragging about while it is connected to the gray box with a HingeJoint2D.

I want to anchor the HingeJoint2D to the exact spot the user is clicking on, and not just the center of the rigidbody.

drag.png (71.6 kB)
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
0
Best Answer

Answer by dimaswift · Feb 17, 2015 at 04:43 PM

You need to create new GameObject (customCursor) as a child of the target object and make it follow your cursor like this:

Vector3 mousePos = Input.mousePosition;

mousePos.z = -10.0f; mousePos = Camera.main.ScreenToWorldPoint(mousePos);

customCursor.transform.position = mousePos;*

and then set HingeJoint position using customCursor's LocalPosition:

HingeJoint2D hinge = gameObject.AddComponent().anchor = pos;

Hope it helps.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer

Question regarding Rect center 0 Answers

Unity 2D: Box Collider Not Working 1 Answer

OnTriggerEnter2D Box Collider 1 Answer

2D Pong Ball Not Reflecting Off Walls "Exactly" 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