Im trying to make a game where the main interaction method is based of the path of the players controller, for example when the controller moves in a square shape, I can do something. I want to know the x and y values relative to the players view.
I needed a break…
You need to track the position of one of the ControllerAnchors over time and evaluate the results. You probably don’t want relative to the player’s view (watch your hand while moving your hand and head, consider the hands offset from center vision, it’s chaos from a positional/rotational perspective). You want to use worldspace (maybe an offest from the main camera).
So, you need a MonoBehavior with a collection to hold positions:
private Dictionary<float, Vector3> previousPositions = new Dictionary<float, Vector3>();
The key is a float, representing the game time of the reading.
Also create a variable to set the maximum time to keep readings for (72fps = 72 readings per second):
public float Duration { get; set; }
In update, prune the list based on Duration and add the new value (someObjectPosition would be the Transform from one of the controller anchors):
// prune list
List posTimesToRemove = previousPositions.Where(p => p.Key < Time.realtimeSinceStartup - Duration).Select(p => p.Key).ToList();
previousPositions = previousPositions.Where(p => !posTimesToRemove.Contains(p.Key)).ToDictionary(p => p.Key, p => p.Value);
float currentTotalTime = Time.realtimeSinceStartup + Time.deltaTime;
if (!previousPositions.ContainsKey(currentTotalTime)) // multiple watchers will attempt to add duplicates
{
previousPositions.Add(currentTotalTime, someObjectPositionTransform);
}
Then the hard part, interpret the data.
Always start from the first point, work on getting an event for a straight horizontal line first. Adjust the duration, comparing the previousPositions.Values.First() and Last() values. (…First().x - …Last().x >= someDistance) AND Mathf.Abs(…First.z - …Last().z < .05f or something, so the motion check is for horizonal motion with minimal vertical, so horizontally moving). This ignores the y axis, so a horizontal line on a projected plane facing out from the starting position (localspace can help with this, still messy but I’m probably not aware of some Unity functions that might help).
Then try and do that for a horizonal + vertical line (split data in center, perform similar checks as above but for each segment, adjust Duration).
Did I mention this was hard…
Very cool things can be done though. I have a bunch of finger/hand rules (bone-to-bone motion - finger flicking, bone-to-bone proximity - thumb-to-finger/fingers on different hands in contact, bone vector motion - think keyboard strokes, hand force pushes). Currently working on controller help panels that fade in after the controller/hand is viewed for a period (1 second, ray from forward on the camera to a collider defined on/around the controller/hand prefabs).