Hey everyone,
I want to scale objects based on the distance between pinched handL & handR.
But I want the ability to keep on scaling the object bigger / smaller when I pinch both hands again.
Similar to the DetectionExamples - PinchMovementDemo which is using the LeapRTS script.
My object currently scales correctly, but every time I pinch my fingers to scale it even bigger, it just pops back and gets small again. I did this with this little piece of code:
selectedObject.transform.localScale = Vector3.one * Vector3.Distance (_pinchDetectorA.Position, _pinchDetectorB.Position) ;
I worked out what to do in theory, but I can't really get it to work.
I would have to store the distance between the first pinched hands.
And then if the distance gets bigger the object has to get bigger and vice versa.
But how would I do that?
In addition to that, I would like to move objects threw my room just by pinching in the air.
I don't want the objects to teleport to my hand position, but rather move them based on my hand movement. Currently the objects move, but teleport to my hand (I know why) but I have no clue how to fix it.
Current code for moving objects:
Vector3 objVectorPos = selectedObject.transform.position;
objVectorPos.x = singlePinch.transform.position.x;
objVectorPos.z = singlePinch.transform.position.z;
selectedObject.transform.position = objVectorPos;
In case someone wants to view the whole script Ill summarize what it does and upload it as well for clarity.
I can choose from multiple objects by pointing at them. I then want to scale the objects by pinching both hands and pulling them apart or closer together (to make an object bigger or smaller).
I also want to move these selected objects by pinching one hand and moving the hand in the air. The object should move from its location according to the hand movement.
~Vulkanos
public class LeapMoveObjects : MonoBehaviour
{
[SerializeField]
private PinchDetector _pinchDetectorA;
public PinchDetector PinchDetectorA {
get {
return _pinchDetectorA;
}
set {
_pinchDetectorA = value;
}
}
[SerializeField]
private PinchDetector _pinchDetectorB;
public PinchDetector PinchDetectorB {
get {
return _pinchDetectorB;
}
set {
_pinchDetectorB = value;
}
}
[SerializeField]
private ExtendedFingerDetector _extendedFinterDetectorL;
public ExtendedFingerDetector ExtendedFingerDetectorL {
get {
return _extendedFinterDetectorL;
}
set {
_extendedFinterDetectorL = value;
}
}
[SerializeField]
private ExtendedFingerDetector _extendedFinterDetectorR;
public ExtendedFingerDetector ExtendedFingerDetectorR {
get {
return _extendedFinterDetectorR;
}
set {
_extendedFinterDetectorR = value;
}
}
private Finger.FingerType FingerName = Finger.FingerType.TYPE_INDEX;
public IHandModel HandModelL;
public IHandModel HandModelR;
public GameObject selectedObject;
private GameObject hitObject;
//if onTarget > 10 hitObject becomes the selectedObject
private int onTarget = 0;
public LineRenderer lr;
private float _defaultNearClip;
void Start ()
{
}
void Update ()
{
bool didUpdate = false;
if (_pinchDetectorA != null)
didUpdate |= _pinchDetectorA.DidChangeFromLastFrame;
if (_pinchDetectorB != null)
didUpdate |= _pinchDetectorB.DidChangeFromLastFrame;
if (didUpdate) {
transform.SetParent (null, true);
}
if (_pinchDetectorA != null && _pinchDetectorA.IsActive &&
_pinchDetectorB != null && _pinchDetectorB.IsActive) {
transformDoubleAnchor ();
} else if (_pinchDetectorA != null && _pinchDetectorA.IsActive) {
transformSingleAnchor (_pinchDetectorA);
} else if (_pinchDetectorB != null && _pinchDetectorB.IsActive) {
transformSingleAnchor (_pinchDetectorB);
} else if (_extendedFinterDetectorL != null && _extendedFinterDetectorL.IsActive) {
Hand hand;
hand = HandModelL.GetLeapHand();
shootRaycast (hand);
} else if (_extendedFinterDetectorR != null && _extendedFinterDetectorR.IsActive) {
Hand hand;
hand = HandModelR.GetLeapHand();
shootRaycast (hand);
} else {
stopShootingRaycast ();
}
if (didUpdate) {
transform.SetParent (selectedObject.transform, true);
}
}
public Transform getAnchor ()
{
return selectedObject.transform;
}
public void shootRaycast (Hand hand)
{
int selectedFinger = selectedFingerOrdinal();
Vector3 firePoint = hand.Fingers[selectedFinger].TipPosition.ToVector3();
lr.enabled = true;
lr.SetPosition(0, firePoint);
//Vector3 fingerDirection = lr.transform.forward;
Vector3 fingerDirection = hand.Fingers[selectedFinger].Bone(Bone.BoneType.TYPE_DISTAL).Direction.ToVector3();
RaycastHit hit;
if (Physics.Raycast(firePoint, fingerDirection, out hit, 2000.0f)) { //was something hit?
fingerDirection = hit.point;
if (hit.collider.tag == "interactable") {
if (hitObject != null && hitObject.Equals (hit.collider.gameObject)) {
onTarget += 1;
} else {
hitObject = hit.collider.gameObject;
onTarget = 0;
}
}
if (onTarget > 10) {
selectedObject = hitObject;
}
}
lr.SetPosition(1, fingerDirection);
}
private int selectedFingerOrdinal(){
switch(FingerName){
case Finger.FingerType.TYPE_INDEX:
return 1;
case Finger.FingerType.TYPE_MIDDLE:
return 2;
case Finger.FingerType.TYPE_PINKY:
return 4;
case Finger.FingerType.TYPE_RING:
return 3;
case Finger.FingerType.TYPE_THUMB:
return 0;
default:
return 1;
}
}
public void stopShootingRaycast()
{
lr.enabled = false;
}
private void transformDoubleAnchor ()
{
selectedObject.transform.localScale = Vector3.one * Vector3.Distance (_pinchDetectorA.Position, _pinchDetectorB.Position) ;
}
private void transformSingleAnchor (PinchDetector singlePinch)
{
Vector3 objVectorPos = selectedObject.transform.position;
objVectorPos.x = singlePinch.transform.position.x;
objVectorPos.z = singlePinch.transform.position.z;
selectedObject.transform.position = objVectorPos;
}
}