- Home /
Question by
Phoera · Feb 02, 2018 at 05:24 PM ·
transform.positionworldspacelocalspace
World to local Inversion not working as I expect
Okay, so I have a chunk of code in the OnDrag, which I use for swiping a gameobject.
public void OnBeginDrag(PointerEventData eventData)
{
GrabPointDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
}
public void OnDrag(PointerEventData eventData)
{
if (allowSwipe && Input.touchCount <= 1)
{
var scrToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var targetWorldPos = new Vector3(scrToWorld.x - GrabPointDir.x, scrToWorld.y - GrabPointDir.y, transform.position.z);
var targetLocalPos = transform.InverseTransformDirection(targetWorldPos);
var clampedLocalPos = new Vector2(Mathf.Clamp(targetLocalPos.x, -100f, 100f), targetLocalPos.y);
transform.localPosition = clampedLocalPos;
}
}
What I expect from this is that I make all calculations in world coordinates, then use InverseTransformDirection to make it local coords and set constraints, then apply the result. But what I get is chaotic movement, jumping etc. I tried both InverseTransformDirection and InverseTransformPoint, but at some point it worked when I set the result to transform.position, not localPosition. So I must be missing something about those transforms.
Comment
Your answer
Follow this Question
Related Questions
How do i move an object relative to another objects axis. 2 Answers
vertices to world position 1 Answer
WorldSpace movement? 1 Answer
About World space and Local space 4 Answers