Need a logic advise

Hello! Im trying to limit some position moving.

                if (hit1.point.x / 2.5f < 190)
                    cornerPoints[2] = new Vector2(hit1.point.x / 2.5f, hit1.point.z / 2.5f);
                else 
                    cornerPoints[2] = new Vector2(190, hit1.point.z / 2.5f);

                if (hit1.point.z/2.5f < 195)
                    cornerPoints[2] = new Vector2(hit1.point.x / 2.5f, hit1.point.z / 2.5f);
                else
                    cornerPoints[2] = new Vector2(hit1.point.x / 2.5f, 195);

Individualy they work perfectly,but if i use them both only the last one will work. How can i combine them,to work them both?

If I understand correctly you want to limit both X and Z to a maximum number (190 and 195 respectively).

Here’s a solution using Mathf.Min:

cornerPoints[2] = new Vector2(Mathf.Min(hit1.point.x / 2.5f, 190), Mathf.Min(hit1.point.z / 2.5f, 195);

What I’m doing here is using for X the smaller of 190, and hit1.point.x / 2.5f, so if the formula is more than 190, it will use 190, if it is smaller, it will use the formula. Same goes with Z.

Your original issue was that the second if overwrote whatever value the first if put, because you created a new vector using both X and Z. If you’d want to keep the original X, you’d use something like this in your second if:

    cornerPoint[2] = new Vector2(cornerPoint[2].x, WHATEVER);

So you’d keep the X value set by your first if, and only set the Y value.