- Home /
How would I make the mouse drag move smoothly?
I have a 2D game with a slingshot mechanic, where the player clicks an object and drags it to aim where it will launch. However, it does not seem to be very smooth. If I move the mouse position very slightly it doesn't change the aiming position until the mouse is a certain distance, then it will "snap" to the next x or y coordinate. I have tried lerping this, but that only gives the appearance of more precise aiming, because it smoothly transitions between the snapped positions, but does not actually allow for the mouse to settle in between snap points. So how would I make the mouse position be more precise? Here is the code I am using for the dragging function.
function OnMouseDrag()
{
var pos = Input.mousePosition;
pos.z = -Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
//Clamps launching object to a circle around the launch point
var newPos = pos + offset;
transform.position = startPos + Vector3.ClampMagnitude((newPos - startPos), radius);
//Clamps launching object from moving too far up or forward
transform.position.x = Mathf.Clamp(transform.position.x, -100, launchPivot.transform.position.x);
transform.position.y = Mathf.Clamp(transform.position.y, -100, launchPivot.transform.position.y);
}
}
I made a discovery after some experimentation and figured out what the issue is, but I am still unsure how to solve it. I did a Debug.Log(pos.ToString("F3")); to see how sensitive the positions were and it would give me results showing that changes were occurring out to several decimal points. For example (1.234, 1.234, 1.234). But when I tried Debug.Log(Input.mousePosition.ToString("F3")); it showed only whole numbers. For example (1.000, 1.000, 1.000).
This may just be my ignorance on how Input.mousePosition works, but could this have something to do with the imprecise mouse position?
thank you , you saved my day by mentioning the problem cause ;
i got a solution to get mouse position with more precision :D
void On$$anonymous$$ouseDown()
{mousePosition = Input.mousePosition;
}
void On$$anonymous$$ouseDrag()
{
mousePosition.x += Input.GetAxis("$$anonymous$$ouse X")*5;
mousePosition.y += Input.GetAxis("$$anonymous$$ouse Y")*5;
print(mousePosition.ToString("F5"));
v2 = Camera.main.ScreenToWorldPoint(mousePosition);
}
Answer by FortisVenaliter · Feb 08, 2017 at 08:29 PM
OnMouseDrag, in my experience, is more for drag and drop. You'd probably be much better off running the code in your update function and track the mouse position/state manually. That way it would match the mouse position every frame regardless.
I thought that sounded like it might fix the issue, but I tried putting the function in Update and unfortunately the problem didn't change. It still snaps to certain positions.
Your answer
Follow this Question
Related Questions
How to prevent GameObject from spawning on top of each other? 1 Answer
Why is OnDrag triggering but not OnMouseDown? 0 Answers
3D mouse position 0 Answers
Ray Over Mouse 1 Answer
How I can save mouse position ? 0 Answers