Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TheGreatS · Jul 20, 2015 at 04:51 AM · rotationclick and dragpysics

2D Click Anywhere on Physics Object and Drag

Right now I'm trying to create a way to click anywhere on a given 2D Rigidbody object and drag it around. I have it working so that when I drag, the object won't go through other objects or jump around too much, etc. The problem I'm having is with the initial clicking. I used to have it so that the object's center snapped to the curser, but that made the object snap to the cursor and it would look unnatural and fling the other nearby objects around.

Now I'm trying to get it so that the part of the object that is clicked on is the part that snaps to the cursor. But this only works some of the time. Sometimes when I click on the object it snapps to where I want and the object won't jump around at all. Other times the object with jump a lot, other times it will jump around only a little. It seems to be caused by the rotation of the object, so I tried to fix that in the code, but the fix didn't appear to do anything.

Here's a video of the problem in action. The Save and Load buttons don't do anything at the moment, so you can just ignore those.

Here is what each of the objects look like in the inspector. They are pretty much the same from object to object. The white rectangles are just sprites with Box Colliders.

And here's the code I'm using in each object:

 using UnityEngine;
 using System.Collections;
 
 public class Dragable: MonoBehaviour {
     
     public SpringJoint2D spring;
     public Rigidbody2D body;
     public Transform objectTransform;
     public bool doPrint;
     
     void Awake()
     {
         
         spring = this.gameObject.GetComponent<SpringJoint2D> (); //"spring" is the SpringJoint2D component that I added to my object
         body = this.gameObject.GetComponent<Rigidbody2D> (); //vairable to access objects rigidbody
         objectTransform = this.gameObject.GetComponent<Transform> (); //variable to access objects transform
         
         spring.connectedAnchor = gameObject.transform.position;//i want the anchor position to start at the object's position
         spring.enabled = false;//make it so the object falls
         body.drag = 0;//make it so the object falls normally
         body.angularDrag = 0;//same as above
     }
     
     
     void OnMouseDown()
     {
 
         float objectRotation;
         objectRotation = objectTransform.eulerAngles.z;//get angle of rotation
         objectRotation = objectRotation * ((2f * Mathf.PI)/360f);//convert to radians
         Vector2 rotationCorrection = new Vector2 (Mathf.Cos (2*objectRotation + Mathf.PI/4) * Mathf.Sqrt (2f),
                                                   Mathf.Cos (2*objectRotation - Mathf.PI/4) * Mathf.Sqrt (2f));//calculate rotation correction
         spring.enabled = true;//I'm reactivating the SpringJoint2D component each time I'm clicking on my object becouse I'm disabling it after I'm releasing the mouse click so it will fly in the direction i was moving my mouse
         Vector2 cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);//getting cursor position
         spring.anchor = Vector2.Scale((cursorPosition - body.position), rotationCorrection); // find position on object and correct for rotation
         body.drag = 10;//decrease spring oscillation 'jiggle'
         body.angularDrag = 5;//same as above
 
     }
     
     
     void OnMouseDrag()        
     {
         if (spring.enabled == true) 
         {
             
             Vector2 cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);//getting cursor position
             
             spring.connectedAnchor = cursorPosition;//the anchor get's cursor's position
             
             
         }
         if (doPrint) {
             Debug.Log(objectTransform.eulerAngles.z);
         }
     }
     
     
     void OnMouseUp()        
     {
         
         spring.enabled = false;//disabling the spring component
         body.drag = 0;//make it so the object falls and moves normaly
         body.angularDrag = 0;//same as above
 
     }
     
 }

Thanks!

Comment
Add comment · Show 3
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 LilGames · May 23, 2017 at 08:16 PM 0
Share

All I know is your code was useful and works for me! Did you ever polish it up how you envisioned?

avatar image LilGames · Jun 12, 2017 at 08:59 PM 0
Share

Did you ever solve the issue with object rotation?

avatar image hexagonius · Jun 13, 2017 at 10:50 AM 0
Share

Didn't want to read it all, but all you need to do is get the offset of the click position (click.pos - object.pos) and subtract it during your drag from the click porn Position to know where the object needs to go.
You can if course reverse the subtraction and add it, just as you like.

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

Can't rotate a parent object by selecting the child 2 Answers

Flip over an object (smooth transition) 3 Answers

Click and Drag rotation similar to Scene View Gizmos 0 Answers

Pysics not working as expected 1 Answer

Why is the animator not changing the position but only changing the rotation? 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