Reputation: 242
I have a simple paddle that I am trying to move along the X axis using the mouse. Currently it moves but is off by a large margin, relative to where the mouse is.
I think it is due the fact that half my screen is -7.5 and the other half is 7.5
I was wondering if there is any way to correct this problem. As you can see from my code I am multiplying by 16, which would be the width if the other half was not negative.
I can move the whole screenplay to make it not negative, so I was hoping there was a function
Vector3 paddlePos = new Vector3 (0f, this.transform.position.y , -0.25f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp(mousePosInBlocks, -7.5f, 7.5f);
this.transform.position = paddlePos;
Upvotes: 0
Views: 2496
Reputation: 1498
Use this code -
Vector3 paddlePos = new Vector3 (0f, this.transform.position.y , -0.25f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp((mousePosInBlocks - 7.5f), -7.5f, 7.5f);
this.transform.position = paddlePos;
Mathf.clamp function only returns the value inside the minimum and maximum value range. While you need the mousePosInBlocks value to consider the negative and positive screen space.
Upvotes: 1