0
\$\begingroup\$

I have a gameobject which i want to move using mouse. Currently i can do that in x-axis and y-axis:

public bool moveDiagonal;
 public bool moveX, moveY;
 public float distanceXmin, distanceXmax, distanceYmin, distanceYmax;
 if (moveX)
 {
     if (curPosition.x >= distanceXmin && curPosition.x <= distanceXmax)
          goToMove.transform.position = new Vector3(curPosition.x, goToMove.transform.position.y, 0);
 }
 else if (moveY)
 {
    if (curPosition.y >= distanceYmin && curPosition.y <= distanceYmax)
          goToMove.transform.position = new Vector3(goToMove.transform.position.x, curPosition.y, 0);
 }

I wanted to move my object diagonally as well how to do that according to its direction using mouse? The object should move along the line or the x and y axis limits.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ have you tried removing the 'else' from your code \$\endgroup\$ – Big T Larrity Nov 27 '18 at 12:44
  • \$\begingroup\$ @SuperMegaBroBro yes i have , this will not make it follow the line , it will move freely \$\endgroup\$ – Shahan Butt Nov 27 '18 at 13:17
0
\$\begingroup\$

I'd make this more generic.

Set up a structure you can populate in the Inspector to specify a direction you're allowed to drag the object, and a minimum and maximum signed distance along this direction that it's allowed to travel from its starting point.

Expose an array of these limits, and tweak them in Start to ensure their direction vectors are all unit length. (You could also use gizmos to visualize the limits in the scene view, or handles to edit them directly)

[System.Serializable]
public struct SlidingLimit {
     public Vector2 direction;
     public float min;
     public float max;
}

[SerializeField]
SlidingLimit[] limits;

Vector2 startPosition;

void Start() {
    startPosition = transform.position;
    for(int i = 0; i < limits.Length; i++) {
        var limit = limits[i];
        limit.direction = limit.direction.normalized;
        limit[i] = limit;
    }
}

Later, inside your drag function, you can call Snap to snap to the closest sliding limit:

Vector2 Snap(targetPosition) {
    if(limits.Length == 0)
         return targetPosition;

    Vector2 offset = targetPosition - startPosition;

    float closestRange = float.PositiveInfinity;
    int closestLimit = -1;

    SlidingLimit limit;

    // Find which line we're closest to,
    // by measuring our perpendicular distance.
    for(int i = 0; i < limits.Length; i++) {
         limit = limits[i];
         float range = offset.x * limit.direction.y - offset.y * limit.direction.x;
         if(range < closestRange) {
               closestRange = range;
               closestLimit = i;
         }
    }

    // Snap the returned point to this line,
    // between its min and max.
    limit = limits[closestLimit];

    float travel = Vector2.Dot(limit.direction, offset);
    travel = Mathf.Clamp(travel, limit.min, limit.max);

    return startPosition + travel * direction;
}

This will let you handle free draggables (no limits in the array), or draggables constrained to one, two, three, or any number of lines, at any angle, with custom travel limits on each, just by configuring your limits array accordingly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.