// Use this for initialization void Start() { drag = GetComponent <DragableBlock>(); blocks = LayerMask.GetMask("Blocks"); rotBy90 = Quaternion.Euler(0, 90, 0); rotByNeg90 = Quaternion.Euler(0, -90, 0); }
// Use this for initialization void Start() { drag = GetComponent<DragableBlock>(); blocks = LayerMask.GetMask("Blocks"); rotBy90 = Quaternion.Euler(0, 90, 0); rotByNeg90 = Quaternion.Euler(0, -90, 0); }
// Update is called once per frame void Update() { if (drag.heldBy != null && touchingMe == 0) { RaycastHit info; if (Physics.Raycast(transform.position, Vector3.down, out info, float.PositiveInfinity, blocks)) { DragableBlock block = info.collider.GetComponent <DragableBlock>(); if (block != null) { Quaternion rot1 = block.transform.rotation * rotBy90; Quaternion rot2 = block.transform.rotation * rotByNeg90; if (Mathf.Abs(Quaternion.Dot(transform.rotation, rot1)) > Mathf.Abs(Quaternion.Dot(transform.rotation, rot2))) { transform.rotation = Quaternion.Lerp(transform.rotation, rot1, Time.deltaTime); } else { transform.rotation = Quaternion.Lerp(transform.rotation, rot2, Time.deltaTime); } } } } }
// Update is called once per frame void Update() { Frame frame = handController.GetFrame(); HandList hands = frame.Hands; if (isUp) { isUp = false; } if (isDown) { isDown = false; } if (isAltUp) { isAltUp = false; } if (isAltDown) { isAltDown = false; } Delta = Vector3.zero; if (hands.Count > 0) { if (currentHand != hands[0]) { currentHand = hands[0]; } Finger indexFinger = currentHand.Fingers.Where(f => f.Type == Finger.FingerType.TYPE_INDEX).SingleOrDefault(); if (indexFinger != null && indexFinger.IsValid) { Vector3 newPosition = indexFinger.TipPosition.ToUnityScaled(); if (Time.frameCount - 1 == lastFramePositionUpdated) { Delta = handController.transform.TransformVector(newPosition - lastPosition); } else { Delta = Vector3.zero; } lastPosition = newPosition; lastFramePositionUpdated = Time.frameCount; } Vector3 middle = Vector3.zero; foreach (Finger f in currentHand.Fingers) { float weight; fingerWeights.TryGetValue(f.Type, out weight); middle += weight * f.TipPosition.ToUnityScaled(); } localBetweenFingers = middle; handRay.origin = handController.transform.TransformPoint(currentHand.PalmPosition.ToUnityScaled()); Vector3 localDir = (currentHand.Direction.ToUnityScaled() + currentHand.PalmNormal.ToUnityScaled()).normalized; handRay.direction = handController.transform.TransformDirection(localDir); //int numberOfExtendedFingers = currentHand.Fingers.Where(f => f.IsExtended).Count(); //Vector3 localDir = ((numberOfExtendedFingers + 1) * currentHand.Direction.ToUnityScaled() + 2 * currentHand.PalmNormal.ToUnityScaled()).normalized; //handRay.direction = handController.transform.TransformDirection(localDir); // COM based on finger tips, pinch becomes hard //Vector3 centerOfFingers = Vector3.zero; //foreach (Finger f in currentHand.Fingers) //{ // centerOfFingers += f.TipPosition.ToUnityScaled(); //} //centerOfFingers /= currentHand.Fingers.Count; //centerOfFingers = handController.transform.TransformPoint(centerOfFingers); //handRay.direction = (centerOfFingers - handRay.origin).normalized; // Promising, basically ends up using the thumb though, kind of shaky when pinching //Dictionary<Finger.FingerType, float> bias = new Dictionary<Finger.FingerType, float>() //{ // {Finger.FingerType.TYPE_THUMB, 6}, // {Finger.FingerType.TYPE_INDEX, 0}, // {Finger.FingerType.TYPE_MIDDLE, 1}, // {Finger.FingerType.TYPE_RING, 1}, // {Finger.FingerType.TYPE_PINKY, 1}, //}; //Vector3 dirOfFingers = Vector3.zero; //foreach (Finger f in currentHand.Fingers) //{ // dirOfFingers += bias[f.Type] * f.Direction.ToUnityScaled(); //} //dirOfFingers.Normalize(); //Vector3 localDir = (currentHand.Direction.ToUnityScaled() + dirOfFingers).normalized; //handRay.direction = handController.transform.TransformDirection(localDir); //Vector3 fingerBoneDir = Vector3.zero; //foreach (Finger f in currentHand.Fingers) //{ // fingerBoneDir += f.Bone(Bone.BoneType.TYPE_METACARPAL).Direction.ToUnityScaled(); //} //fingerBoneDir.Normalize(); //handRay.direction = handController.transform.TransformDirection(-fingerBoneDir); if (currentHand.PinchStrength >= PinchActivation && !isHeld) { isDown = true; isHeld = true; } else if (currentHand.PinchStrength < (PinchActivation - PinchRealse) && isHeld) { isUp = true; isHeld = false; } if (currentHand.GrabStrength >= GrabActivation && !isAltHeld) { isAltDown = true; isAltHeld = true; } else if (currentHand.GrabStrength < (GrabActivation - GrabRelease) && isAltHeld) { isAltUp = true; isAltHeld = false; } GestureList gestures = frame.Gestures(); foreach (Gesture g in gestures) { if (g.Type == Gesture.GestureType.TYPE_CIRCLE) { CircleGesture cg = new CircleGesture(g); if (g.State == Gesture.GestureState.STATE_START) { lastProgress = cg.Progress; } if (g.State == Gesture.GestureState.STATE_UPDATE) { float degrees = -Mathf.Sign(Vector3.Dot(cg.Normal.ToUnityScaled(), cg.Pointable.Direction.ToUnityScaled())) * (cg.Progress - lastProgress) * 360; DragableBlock block = DragableBlock.held.FirstOrDefault(); if (block != null) { block.transform.localRotation *= Quaternion.Euler(0, RotationSpeed * degrees, 0); } } lastProgress = cg.Progress; } } } else { currentHand = null; if (isHeld) { isHeld = false; isUp = true; } if (isAltHeld) { isAltHeld = false; isAltUp = true; } } }