- Home /
How to click and drag an object at full speed
I can click and drag my objects like I want but if I move my mouse to fast I lose the object and then I have to go click it again to drag it. How can I make sure my object stays with my mouse? Here is my current code;
if(Input.GetMouseButton(0))
{
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit))
{
if(hit.transform.gameObject == selectedObject)
{
selectedObject.transform.position.x = hit.point.x;
selectedObject.transform.position.y = hit.point.y;
}
}
}
Have you considered using On$$anonymous$$ouseDrag() ?
Answer by robertbu · Jul 23, 2013 at 07:18 AM
Your problem is happening because the mouse is getting ahead of the object. When that happens, the Raycast() fails, so the drag stops. The only way I can think of to fix this problem is to change it so that the initial raycast starts the dragging, but after that it uses something else to move the cube. Here is my quick take on fixing the problem. The code got a whole lot more complicated, but I could not think of a simple fix:
private var dist : float;
private var toDrag : Transform;
private var dragging = false;
private var offset : Vector3;
function Update() {
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var v3 : Vector3;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit))
{
if(hit.transform.gameObject == selectedObject)
{
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
}
if (Input.GetMouseButton(0))
{
if (dragging)
{
v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
}
if (Input.GetMouseButtonUp(0))
{
dragging = false;
}
}
It works almost perfectly. The only problem is its inverted. if i move my mouse up my part moves down. and vice versa. I tried playing with the code and i can't fix. Any idea? And thanks by the way you helped me out a ton man.
I'll bet you have your camera at positive 'z' looking back at negative 'z'. You can fix it in a couple of ways. Change line 17 to:
dist = $$anonymous$$athf.Abs(hit.transform.position.z - Camera.main.transform.position.z);
or
dist = Camera.main.transform.position.z - hit.transform.position.z;
The issue is that Camera.main.ScreenToWorldPoint() must get the positive distance in front of the camera, and the initial calculation was returning a negative distance for a camera that was not in the default position/orientation.
Thanks a ton man. It works perfect. I learned a lot right here. Thanks again :)
This has been really useful, thank you both. However, why does it drag slower when the camera is on the side ins$$anonymous$$d of the front or the back of the object?
Thanks alot, I was searching for a while now. I was wondering, how you can reduce the dragspeed?
Answer by Tsilliev · Mar 31, 2018 at 07:28 AM
If your mouse pointer is getting ahead of the object thus losing the collider of the object here is an easy solution without offsets. The solution is that on mouse button down, store the last clicked object, and on mousebutton (constant execution) drag the object.
GameObject DraggedObj;
void Update()
{
if (Input.GetMouseButtonDown (0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
if (hit.transform.gameObject.tag == "Card")
{
DraggedObj = hit.transform.gameObject;
}
}
}
if (Input.GetMouseButton(0))
{
if(DraggedObj != null)
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.z = DraggedObj.transform.position.z;
DraggedObj.transform.position = point;
}
}
else if (Input.GetMouseButtonUp(0))
{
OnMouseUp ();
DraggedObj = null;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110642im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to drag a GameObject with touch but at a limited speed 1 Answer
How will i drag my object only x and y axis? 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers