I’m trying to figure out exactly what iPhoneTouch.deltaTime represents in Unity iPhone 1.5, and i’m confused. In a device build, whenever phase == Stationary, touch.deltaTime seems to either adopt the last known time.deltaTime from a move event, or be zeroed out when the last event was a Began phase.
In the Remote, it seems to either use the last known touch.deltaTime if it was a Moved phase event, or it just picks up a value, but i’m not sure from where.
To see what i’m talking about, create a new scene and attach the following two scripts to mainCamera:
First, DebugWindow.js to get a realtime scrolling view of the touch events on the screen:
static var debugLines : Array;
static var maxLines : int = 20;
var debugSkin : GUISkin;
function Start() {
}
function Awake() {
debugLines = new Array();
AddLine("Begin Debugging");
}
function OnGUI () {
GUI.skin = debugSkin;
var boxWidth = 300;
var boxHeight = 280;
var pad = 5;
if (debugLines) {
var assembledString : String = debugLines.Join("\n");
GUI.Box(Rect(Screen.width - boxWidth - pad, pad, boxWidth, boxHeight), assembledString);
}
}
public static function AddLine(msg : String) {
Debug.Log("[DW] : " + msg);
debugLines.Add(String.Format("{0:#.###}", Time.time) + ": " + msg);
if (debugLines.length >= maxLines) {
debugLines.Shift();
}
}
Next, UITouchTesting.js which examines touch events and adds them to our screen debug window:
function Update () {
if (iPhoneInput.touchCount > 0 (iPhoneInput.GetTouch(0).phase != iPhoneTouchPhase.Canceled)) {
var touch : iPhoneTouch = iPhoneInput.GetTouch(0);
if (touch.phase == iPhoneTouchPhase.Began) {
DebugWindow.AddLine("Touch began");
} else if (touch.phase == iPhoneTouchPhase.Moved) {
DebugWindow.AddLine("Touch moved: " + touch.deltaPosition + ", " + touch.deltaTime);
} else if (touch.phase == iPhoneTouchPhase.Stationary) {
DebugWindow.AddLine("Touch still: " + touch.deltaPosition + ", " + touch.deltaTime);
} else if (touch.phase == iPhoneTouchPhase.Ended) {
DebugWindow.AddLine("Touch ended: " + touch.deltaPosition + ", " + touch.deltaTime);
}
}
}
Is this a bug in the Unity engine? Is phase == Stationary supposed to always have the deltaTime from the Stationary event’s firing to the previous touch event’s timestamp? That way, if we add up all event deltaTimes from a series of touch events, it should approximately be the same as the difference in Time.time between the frames where we captured those events.
If Stationary deltaTime is missing, aggregating touch.deltaTime values is going to be off during touches that stay in one place for a while. It does appear that the deltaTime values for Moved events are accurate. They won’t add up exactly to the Time.time differences because the Events probably happen and have timestamps set apart from the Time.time available in every Update() run.