-How to only allow paddle to move if the mouse is within a certain range

So I am making a level with two paddles, however I only want each paddle to work if the mouse is within a certain distance from the object. I am struggling on how to do this.
public class Paddle : MonoBehaviour
{
[SerializeField] float screenWidthInUnits = 16f;
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 13.5f;
[SerializeField] int numberOfPaddles;
public float distancePaddleCanWork = 4f;
//cached ref
GameStatus theGameStatus;
Rocket theRocket;
// Start is called before the first frame update

void Start()
{
    theGameStatus = FindObjectOfType<GameStatus>();
    theRocket = FindObjectOfType<Rocket>();
}

// Update is called once per frame
void Update()
{
   
    Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    if (numberOfPaddles > 1)
    {
        if(distancePaddleCanWork = transform.position -Input.mousePosition)
    }
    else
    {
        paddlePos.x = Mathf.Clamp(GetXPos(), minX, maxX);
        transform.position = paddlePos;
    }
}
private float GetXPos()
{
    if (theGameStatus.IsAutoPlayEnabled())
    {
        return theRocket.transform.position.x;
    }
    else
    {
       return Input.mousePosition.x / Screen.width * screenWidthInUnits;
    }
}

}

1 Like

This is a little tricky to pull off, this is all about vector math.

transform.position - Input.mousePosition

The line of code above won’t work. It will give you a vector instead of a float, so you can’t compare that. It’ll return a very, very weird vector, because the mousePosition returns the screen position, not the world position, so you have to convert it first.

To convert your mouse position to world position you’ll have to use something similar to this:

Vector3 mousePosition = Input.mousePosition;
Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(mousePosition);

Camera.main.ScreenToWorldPoint() will convert your mouse position to world position, as the name implies.

After that is a matter of calculating the distance between the two vectors, to do that Unity has a very useful and simple method: Vector2.Distance, you can use this but keep in mind that is not optimized, it uses the square root operation which you want to avoid like the plague, well, in this case, it wouldn’t be much of an issue since the game is simple.


If you want to calculate the distance without using that operation you’ll need to do some calculations.

Subtracting two points (or vectors) will give you the vector between the two, a vector tells you a direction and also a distance (better known as magnitude).

float squaredDistance = (mouseWorldPosition - paddle.Positon).sqrMagnitude;

This will return you the squared distance between the paddle and the mouse position, so you’ll need to compare that to your squared distancePaddleCanWork.

if (squaredDistance <= distancePaddleCanWork * distancePaddleCanWork)

This might look a little confusing and convoluted compared to using Vector2.Distance, but it’s much better performance-wise.

Hope this helps.

1 Like

Thank you so much! I was looking all over online and I couldn’t understand or see it. You explained this very well, my only thing is for paddle.Position it says that Vector2 does not contain a definition for Position, do you know why that would be?

Oh, that was just an example, you can change that to transform.position or any variable that holds the transform information of the paddle.

1 Like