0
\$\begingroup\$

So the player can move in four direction - forward, backward, right and left. But the forward direction should be from wherever he turns his gear VR controller.

I have tried the below code, but when I point the controller upward, the player moves upward. I tried someQuaternion.y as well, but it didn't work as expected. What should I do?

     Quaternion someQuaternion = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote);
           Vector2 dir =  GetDirection(OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad, OVRInput.Controller.RTrackedRemote));
            if(dir == Vector2.right)
            {
                transform.Translate(someQuaternion * Vector3.right * currentSpeed * Time.deltaTime);

            }
            else if(dir == Vector2.left)
            {
                transform.Translate(someQuaternion * -Vector3.right * currentSpeed * Time.deltaTime);

            }
            else if (dir == Vector2.up)
            {
                transform.Translate(someQuaternion * Vector3.forward * currentSpeed * Time.deltaTime);

            }
            else if (dir == Vector2.down)
            {
                transform.Translate(someQuaternion * -Vector3.forward * currentSpeed * Time.deltaTime);

            }


public Vector2 GetDirection(Vector2 input)
    {
        Vector2[] directions = new Vector2[] {
        Vector2.up,
        Vector2.right,
        Vector2.down,
        Vector2.left
    };

        Vector2 direction = Vector2.zero;
        float max = Mathf.NegativeInfinity;

        foreach (Vector2 vec in directions)
        {
            float dot = Vector2.Dot(vec, input.normalized);

            if (dot > max)
            {
                direction = vec;
                max = dot;
            }
        }

        return direction;
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.