public override void OnFrame(InteractionController controller) { if (IsGrabbed) { //disable interaction once the hand leaves the region of interest //var hand = controller.GetHand(); //float distance = MathUtil.Distance(controller.Target.transform.position, gameObject.transform.position); //if (distance > MAX_INTERACTION_DISTANCE) //{ // controller.Drop(); // return; //} //rotate the oven door such that it is as close to the hand as possible //this motion is described by finding the point closest to another (on a circle) var hand = controller.GetHand(); var closestPoint = MathUtil.ClosestPointOnCircle(hand.transform.position, DoorHingeLocation, DoorHingeToHandleRadius); //if the hand is too far from that point, drop the door float distanceFromHandToHandle = MathUtil.Distance(hand.transform.position, closestPoint); if (distanceFromHandToHandle > MAX_INTERACTION_DISTANCE) { controller.Drop(); return; } //calculate door rotation angle float diffZ = (closestPoint.z - DoorHingeLocation.z) / DoorHingeToHandleRadius; float angle = Mathf.Asin(diffZ); //perform rotation Door.transform.rotation = Quaternion.Euler(angle, 0f, 0f); //hide hand var handRenderer = hand.GetComponent <MeshRenderer>(); handRenderer.enabled = false; ////rotate the dial as the hand rotates //float targetAngle = controller.Target.transform.rotation.eulerAngles.z - StartEulerRotation.z; //targetAngle *= -1; //targetAngle = Mathf.DeltaAngle(0f, targetAngle); // converts to -180..180 //float originalAngle = Mathf.DeltaAngle(0f, StartEulerRotation.z); // converts to -180..180 //float diffAngle = Mathf.DeltaAngle(originalAngle, targetAngle); // total diff //if (targetAngle > ANGLE_ZERO_DEADBAND && targetAngle < 0) //{ // //round to zero // targetAngle = 0; //} //if (targetAngle > 0 && targetAngle < ANGLE_MAX) //{ // //round to nearest // if (ANGLE_MAX - targetAngle > targetAngle) targetAngle = 0; // else targetAngle = ANGLE_MAX; //} //gameObject.transform.rotation = Quaternion.Euler(new Vector3(0, 0, targetAngle)); } }
public override void OnFrameWhenGrabbed(InteractionController controller, GameObject hand) { //disable interaction once the hand leaves the region of interest float distance = MathUtil.Distance(controller.Target.transform.position, gameObject.transform.position); if (distance > MAX_INTERACTION_DISTANCE) { controller.Drop(); return; } //hide hand var handRenderer = controller.HandRenderer; handRenderer.enabled = false; //rotate the dial as the hand rotates float targetAngle = controller.Target.transform.rotation.eulerAngles.z - startEulerRotation; targetAngle *= -1; targetAngle = Mathf.DeltaAngle(0f, targetAngle); // converts to -180..180 if (targetAngle > ANGLE_ZERO_DEADBAND && targetAngle < 0) { //round to zero targetAngle = 0; } if (targetAngle > 0 && targetAngle < ANGLE_MAX) { //round to nearest if (ANGLE_MAX - targetAngle > targetAngle) { targetAngle = 0; } else { targetAngle = ANGLE_MAX; } } FireObject.SetActive(targetAngle != 0); Vector3 rot = new Vector3(gameObject.transform.localRotation.x, gameObject.transform.localRotation.y, gameObject.transform.localRotation.z); rot.z = targetAngle; gameObject.transform.localRotation = Quaternion.Euler(rot); }
public override void OnFrame(InteractionController controller) { if (IsGrabbed) { //disable interaction once the hand leaves the region of interest var hand = controller.GetHand(); float distance = MathUtil.Distance(controller.Target.transform.position, gameObject.transform.position); if (distance > MAX_INTERACTION_DISTANCE) { controller.Drop(); return; } //hide hand var handRenderer = hand.GetComponent <MeshRenderer>(); handRenderer.enabled = false; //rotate the dial as the hand rotates float targetAngle = controller.Target.transform.rotation.eulerAngles.z - StartEulerRotation.z; targetAngle *= -1; targetAngle = Mathf.DeltaAngle(0f, targetAngle); // converts to -180..180 float originalAngle = Mathf.DeltaAngle(0f, StartEulerRotation.z); // converts to -180..180 float diffAngle = Mathf.DeltaAngle(originalAngle, targetAngle); // total diff if (targetAngle > ANGLE_ZERO_DEADBAND && targetAngle < 0) { //round to zero targetAngle = 0; } if (targetAngle > 0 && targetAngle < ANGLE_MAX) { //round to nearest if (ANGLE_MAX - targetAngle > targetAngle) { targetAngle = 0; } else { targetAngle = ANGLE_MAX; } } gameObject.transform.rotation = Quaternion.Euler(new Vector3(0, 0, targetAngle)); } }