- Home /
 
Transform translate speed of paddle depending on mouse speed
Hi. I am new in unity3d and I am creating an easy game where is paddle and ball that can be reflected. Movement of the paddle:
 if (Input.GetAxis("Mouse X") < 0)
 {
     transform.Translate(-Time.deltaTime * 18, 0, 0);
 }
 if (Input.GetAxis("Mouse X") > 0)
 {
     transform.Translate(Time.deltaTime *18, 0, 0);
 }
 
               but i need to set movement speed of paddle same as a speed of mouse. How can I do it? Is there any mouse sensitivity / speed to add?
               Comment
              
 
               
              Answer by robertbu · Jan 10, 2014 at 06:44 PM
The typical way to solve this problem would be to convert the mouse position into a world position and set the 'x' of your object to the converted position:
 #pragma strict
 
 function Update() {
     var pos = Input.mousePosition;
     pos.z = transform.position.z - Camera.main.transform.position.z;
     pos = Camera.main.ScreenToWorldPoint(pos);
     transform.position.x = pos.x;
 }
 
               If this is some sort of Pong game and you are using a Rigidbody for the ball, consider making the paddle a Rigidbody also and using Rigidbody.MovePosition() to set the position.
Your answer