- Home /
Unity - dragging a RigidBody2D Gameobject by mouse/touch without any lag
I am creating a 2D game in Unity and wanted to see if anyone has any suggestion on how I can improve the script below which I am using to drag a paddle in a breakout/arkanoid style game. I know there are more complex ways to drag objects but this works ok for me but the only issue I encounter is that when I test my game on a mobile device the dragging is not 100% sharp and when I stop dragging the paddle seems to lag ever so slightly. I don’t have any issues with my mobile device as I have played other breakout games I downloaded from the Play store and the dragging is very crisp.
The script below is attached to the paddle. Basically, I want to be able to drag the paddle with minimal or zero lag.
 Vector3 dist;
 float posX;
 float posY;
 
 void OnMouseDown(){
     dist = Camera.main.WorldToScreenPoint(transform.position);
     posX = Input.mousePosition.x - dist.x;
     posY = Input.mousePosition.y - dist.y;
 
 }
 
 void OnMouseDrag(){
     Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z);  
 
     Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
 
     transform.position = worldPos;
 }
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making 2d joystick correct to controls (mobile) 0 Answers
Move 2D Sprite Horizontally With Mouse 1 Answer
onControllerHide() crash 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                