Thursday, October 6, 2022
HomeGame DevelopmentUnity: Tips on how to convert place between mother or father and...

Unity: Tips on how to convert place between mother or father and youngster remodel in an accurate manner?


(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 issue is, what positional information ought to I get/set from the dragging object?
// If utilizing anchorPosition is improper, 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 the way to map the place to youngster object underneath drag?
// Beneath doesn't work: place is flickering.
// dragRectT.anchorPosition = dragRectT.InverseTransformPoint(ancestorRectT.TransformPoint(newPositionInAncestor)));
dragRectT.anchorPosition = ???
// Or utilizing anchorPosition is improper, then what ought to I exploit?
  1. What’s the appropriate option to contrain drag place?
    e.g. I wish to clamp drag area to ancestor rectangular sure. (In order that mouse can transfer past sure, however drag object stops at sure)
// Beneath 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);

Notice:
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

- Advertisment -
Google search engine

Most Popular

Recent Comments