I am trying to create a pong type game and I have ran into a problem. I want the player to be able to drag the paddle along the y-axis but the game object is not behaving the way I want it to. Here is my code:
private Vector3 screenPoint;
private float startPosX;
private float startPosZ;
void Update ()
{
startPosX = -10.0f;
startPosZ = 0f;
transform.position = new Vector3(startPosX, Input.mousePosition.y, startPosZ);
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
}
void OnMouseDrag()
{
Vector3 currentScreenPoint = new Vector3(startPosX, Input.mousePosition.y, screenPoint.z);
Vector3 currentPos = Camera.main.ScreenToWorldPoint(currentScreenPoint);
transform.position = currentPos;
}
I only want the paddle to move along the Y-Axis but it seems that when I try to drag the object it suddenly appears off the game screen. Is there another way to do something like this? Or am I doing this wrong?