Check if player is doing a nod or shake with camera.

Hello forum,

I recall there was a really cool script somewhere… and unfortunately I lost my copy a couple of months ago.
The script detected whether the player was performing a nod or head shake with the camera.
Does anyone have an example on how to tackle this or still have a copy of the script in question?

Thanks in advance!

Well, in theory, you need a recording device that is sampling camera movement over time, but only does so for the last n seconds, overwriting anything older. It is capable of tracking two things on the fly: 1) the accumulated (absolute) delta movement per axis, and 2) the point average (to differentiate legit turning from moving around the same directional vector).

The shake (or nod, which is when you exclude one axis) is registered when the average doesn’t move over some threshold, while the accumulated delta is high, but obviously you need to tweak the three variables: 1) the time interval, 2) the delta threshold, and 3) the average tolerance, to make this work.

This sounded incredibly interesting so I thought I’d take a whack at it myself.

Give it a try. Here it is in action:

Full sources at:

It is two scripts, one for yes, one for no, hack it up or twiddle the settings however you like.

Who knows, it might even work in VR…

2 Likes

Collect a small list of camera floats Y and Z, And check its contents for the desired wav length pattern to emerge. Or for the contents to contain both max and min of the desired wave length.

this would b a very interesting mechanic indeed.

1 Like

A little late to the party but for anyone still looking at this, I came up with a pretty simple script solution here. it just tracks the cumulative motion in two axes and creates two values, negative for motion in the horizontal “nx”, positive for motion in the vertical “ny”. These are summed so that the two axes counteract, this is meant to make ambiguous nods or circular nods stay as neutral responses. Once nod > 1f is reached that is a definitive “yes” and -1 means “no”. You can easily try this for yourself by putting this script on a fps controlled camera.

This is set up so that you press space key to start tracking, and it stops once a conclusion is reached. The properties “sensing” and “a” are for tuning the system, I found that sensing = 1 and a = 3 works well.

public class NodTesting : MonoBehaviour
{
    public bool prompted;
    public float sensing = 1f;
    public float a = 1f;

    Vector3 oldView;
    Vector3 iView;
    float h_cr;
    float h_disp;
    float v_cr;
    float v_disp;

    public float nod;

    void Update()
    {
        if (Keyboard.current.spaceKey.wasPressedThisFrame)
        {
            prompted = true;
            iView = transform.forward;
            h_cr = 0f;
            h_disp = 0f;
            v_cr = 0f;
            v_disp = 0f;
        }

        if (prompted)
        {
            Vector3 view = transform.forward;
            h_cr += a * Mathf.Deg2Rad * Vector3.Angle(Vector3.ProjectOnPlane(view, Vector3.up), Vector3.ProjectOnPlane(oldView, Vector3.up));
            h_disp = 1/a * Mathf.Deg2Rad * Vector3.Angle(Vector3.ProjectOnPlane(view, Vector3.up), Vector3.ProjectOnPlane(iView, Vector3.up));
            v_cr += a * Mathf.Abs(view.y - oldView.y);
            v_disp = 1/a * Mathf.Abs(view.y - iView.y);
            float nx = Mathf.Max((1 / sensing) * -Mathf.Log(h_disp + 1 / (1 + h_cr)), 0f);
            float ny = Mathf.Max((1 / sensing) * - Mathf.Log(v_disp + 1 / (1 + v_cr)), 0f);

            nod = ny - nx;
        }

        if (nod > 1f)
        {
            Debug.Log("positive");
            prompted = false;
            nod = 0f;
        }
        if(nod < -1f)
        {
            Debug.Log("negative");
            prompted = false;
            nod = 0f;
        }
            
        oldView = transform.forward;
    }
}