Tuesday, April 16, 2024
HomeSample Page

Sample Page Title


(Unity coord system appears to be extraordinarily complicated and documentation is ineffective.)
For instance, I wish to implement a easy drag in 2D UI.

  1. So I wish to deal with the drag in ancestor coord, after which convert again to dragging object.
    The drawback is, what positional information ought to I get/set from the dragging object?
// If utilizing anchorPosition is incorrect, then what ought to I exploit?
startObjectPositionInAncestor = dragRectT.anchorPosition;
...
// The place in ancestor works as anticipated.
RectTransformUtility.ScreenPointToLocalPointInRectangle(ancestorRectT, screenInputPoint, canvas.worldCamera, out Vector2 pointInAncestor);
var newPositionInAncestor = (pointInAncestor - startPointInAncestor) + startObjectPositionInAncestor;
// Now tips on how to map the place to youngster object beneath drag?
// Below doesn't work: place is flickering.
// dragRectT.anchorPosition = dragRectT.InverseTransformPoint(ancestorRectT.TransformPoint(newPositionInAncestor)));
dragRectT.anchorPosition = ???
// Or utilizing anchorPosition is incorrect, then what ought to I exploit?
  1. What is the right approach to contrain drag place?
    e.g. I wish to clamp drag area to ancestor rectangular certain. (So that mouse can transfer past certain, however drag object stops at certain)
// Below doesn't work: the clamp area is displaced from the visible area.
// newPoint.x = Mathf.Clamp(pointInAncestor.x, ancestorRectT.rect.xMin, ancestorRectT.rect.xMax);
// newPoint.y = Mathf.Clamp(pointInAncestor.y, ancestorRectT.rect.yMin, ancestorRectT.rect.yMax);

Note:
I’m modding so I can’t reproduce it in editor or present screenshot of object properties in editor.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments