- Home /
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!
All I know is your code was useful and works for me! Did you ever polish it up how you envisioned?
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.
Your answer
Follow this Question
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