- Home /
old
Grab and drag object in physical and realistic way
Is there any method that I can grab object from any point on object and drag them realistic way. When I grab it, I want it to settle down by mass axis. Like this:
Then when it's accelerating. It goes like this:
When I tried to do this with springjoin2D, I cant make the same behaviour like above. Then, I did this with hinge joint but I can't avoid this object from overlapping with other object, is there any other way to do this? or Where did I make mistake here is my script.
here I tried to achive connected anchor to follow my mouse position. Object behavior like above but with one mistake, object can overlap collision area of other object.
I understand why this problem occur: Because I am teleporting object to mouse position so it ignores collision effect... But I cant figure out the solution
using UnityEngine;
using System.Collections;
public class MouseJoint : MonoBehaviour {
Vector2 mousePos2D;
SpringJoint2D sj;
void Start() {
sj = gameObject.GetComponent<SpringJoint> ();
}
// Update is called once per frame
void Update () {
Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
}
void FixedUpdate ()
{
sj.connectedAnchor = mousePos2D;
}
}
I thing it is very fundemental to grab object like this, and drag it physically. I am very beginner to unity And sorry for my bad english
Answer by saud_ahmed020 · May 24, 2016 at 07:37 AM
@omer1476 You can get this functionality by using new UI system. Just make a UI Canvas and make new image attach your sprite to this image. After this attach the following script to your image and register the methods that are in script.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class DragObject : MonoBehaviour {
private Vector2 prevDragPos;
public void OnBeginDrag(BaseEventData data)
{
prevDragPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
public void OnDrag(BaseEventData data)
{
Vector3 curPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
float x = curPos.x - prevDragPos.x;
float y = curPos.y - prevDragPos.y;
Vector3 resPos = transform.position;
resPos.x += x;
resPos.y += y;
transform.position = resPos;
prevDragPos = curPos;
}
}
Thank you, I used new canvas, ı created new image to this canvas, I attached my sprite to this image as a source image. I added this script to this image. But I don't understand how I register method. Should I use another script for my rigidbody or should I use this method in another place?
@omer1476 Add Event Trigger component to this Image and then register methods.
What do you mean by "register method". Do you suggest me to subscribe this method to the event. Where should I register this methods, can you give me a simple example? I just read a book for c#, $$anonymous$$aybe I'm very beginner of c# too... I dont know the concept of "registering method". How do I learn this concept, how can I apply this in my project? Thank you again
Does this really answers the question? Why is the answer marked as correct? There's nothing here using physics (Unity's or custom), so I can't imagine how dragging something will make it move like it's hanging from a point. It does work for dragging something, but that's just part of the question.