Im having an issue trying to code up a camera panning solution using the new ui events system. Check out this video first:
As you can see when I pan the camera the HUD jitters as if its position is off by one frame.
I have setup my canvas to be screen space camera as I need some game stuff to be rendered in front of it.
If I set the canvas to be screen space overlay then the problem doesnt present itself.
My panner object is a large 2d collider game object that has its z order set behind the game objects:
The Panner code is very simple:
public class Panner : MonoBehaviour, IDragHandler
{
public Transform target;
private float unit;
public void Awake()
{
var p1 = UnityEngine.Camera.main.ScreenToWorldPoint(Vector3.zero);
var p2 = UnityEngine.Camera.main.ScreenToWorldPoint(Vector3.right);
unit = Vector3.Distance(p1, p2);
}
public void OnDrag(PointerEventData data)
{
target.Translate(-data.delta.x * unit, -data.delta.y * unit, 0f);
}
}
Does anyone have any idea why its jittering?
I thought it may be an update order issue so I tried moving the translate code out of the panner into an āLateUpdateā method but it still had the same problem.
Im not actually translating the Canvas, im translating the Camera transform.
Despite that I tried Canvas.ForceUpdateCanvases and it didnāt work plus it seems to āincrease the sensitivityā i.e. when you move the mouse 1px it translates by 2, probably because the drag event is now being fired twice.
Itās the weekend: Wait until the devs who actually wrote the code come back and see this. The rest of us are still learning the system by trial-and-error-and-partial-sourcecode.
var p1 = UnityEngine.Camera.main.ScreenToWorldPoint(Vector3.zero);
var p2 = UnityEngine.Camera.main.ScreenToWorldPoint(Vector3.right);
unit = Vector3.Distance(p1, p2);
can anyone explain what this code is for and why my orthographic 2d camera pan does not work without multiplying delta by this ālength of a unity vectorā?
it works and that is great but took me a while to āfigure it outā