Question

I have a Vector2-variable where vector.x and vector.y have been clamped so that both are only allowed a value between 0 and 4. Obviously, this leaves me to having a quadratic space around the coordinate 2, 2 to move a gameobject around on. But what I want to achieve is having a circular space to move a gameobject on, which means that I need it to clamp the value of vector.x+vector.y to be a total value in between 0 and 4, but I can't seem to make it work. Is there another function I should use instead of clamp?

This is what I have at the moment:

Vector2 pos = rigidbody2D.transform.position;
pos.x = Mathf.Clamp (pos.x, 19.5f, 24);
pos.y = Mathf.Clamp (pos.y, 3.3f, 6);
rigidbody2D.transform.position = pos;

Instead of clamping each axis individually, how can I give them a total minimum- and maximum-value?

Was it helpful?

Solution

There is a similar function for Vectors: Vector2.ClampMagnitude and Vector3.ClampMagnitude

You can only specify a maximum length, so you have to take care of the minimum yourself. The problem with the minimum value is, that the function would not know what to do if the vector has a length of 0. In which direction should the vector point to achieve the minimum length?

Limit input to a circle

If you only want to limit the movement to a circle, you don't need a minimum value. Instead define the center as (2,2) and limit the movement to a radius of 2.

Vector2 center = new Vector2(2f, 2f);
Vector2 moveBy = new Vector(4f, 7f); // set this to whatever your input is

moveBy = Vector2.ClampMagnitude(moveBy, 2f);

Vector2 newPosition = center + moveBy;

newPosition will be inside a circle with a radius of 2 around your center at (2,2)

Limit given position to a circle

If you want to clamp a given position to a circle, you can slightly modify the version above. It's like putting the object on a leash and pull it back when it leaves the circle.

Vector2 center = new Vector2(2f, 2f);
Vector2 position = new Vector2(6f, 5f); // outside your desired circle

Vector2 offset = position - center;
Vector2.ClampMagnitude(offset, 2f);

position = center + offset;

OTHER TIPS

Unfortunately There is no in-built function available same as clamp function. but if you don't want to use CLAMP function then you can use IF statements.

if(pos.x > 19.5f )
{
  //put logic here
}
if( pos.x < 24f)
{
  //put logic here
}
if(pos.y > 3.3f) 
{
      //put logic here
}
if( pos.y < 6f)
{
   //put logic here
}

you can do this. but in my opinion Mathf.Clamp() is the best option for Restricting values in unity,if conditions will not be accurate as Mathf.Clamp() Function.

Not C#, but here's a snippet I use for limiting the touchpad knob to the area of the touchpad in a libgdx project of mine:

    Vector2 maxPos = new Vector2(screenX, screenY);
    if (maxPos.dst(new Vector2(Settings.touchpadPositionX, Settings.touchpadPositionY)) > painter.getTouchpadSize())
    {
        maxPos.sub(Settings.touchpadPositionX,Settings.touchpadPositionY);
        maxPos.nor();
        maxPos.scl(painter.getTouchpadSize());
        maxPos.add(Settings.touchpadPositionX,Settings.touchpadPositionY);
    }
    painter.updateTouchpadPos((int)maxPos.x, (int)maxPos.y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top