The problem seems simple enough to solve but I cannot wrap my head around how to get it to work.
I have a GameObject, which I want to move from 0 to 12 on the Z axis. I want this to be in accordance with the palm position of the hand.
The hand (for arguments' sake) moves in a reaching movement from 150 to -150 Leap units (the Leap Z axis is reversed to the Unity Z axis). So I want the GameObject to move from 0 (at 150 Leap units) to 12 (at -150 Leap units).
The stickler is; i need this to be configurable no matter what the two Leap values are. 120 to -20, 200 to 0, 80 to -220, for example.
Can anyone help!?
Answer by Charris4
Mar 20 at 06:00 PM
See if this works: float leapInput; //the inverted leap input float leapMax; //The maximum float leapMin; //The minimum float leapNorm; //The normalized (0-1) value of leapInput GameObject myGameObject; //The GameObject you're trying to move
//This needs to happen every time you get a new value from leap controller
leapInput = [direct leap input] * -1f //Inverses leap values
//******** Not sure if you meant this had to be defined on the fly or not, but this should do it.
//Normalize leap values based on min and max
if (leapInput < leapMin) //min
leapMin = leapInput
if (leapInput > leapMax) //max
leapMax = leapInput
leapNorm = (leapInput - leapMin) / (leapMax - leapMin) //perform normalization
leapNorm * 12f //Multiply the normalized leap value by your scale
myGameObject.transform.position.z = leapNorm;
Answer by elenzil
Mar 20 at 07:39 PM
Lerp and InverseLerp.
Vector3 gameObjPt1 = new Vector3(0, 0, 0);
Vector3 gameObjPt2 = new Vector3(0, 0, 12);
float leapPt1 = -150;
float leapPt2 = 150;
float leapVal = <get the current value from leap>;
float leapValNormalized = Mathf .InverseLerp(leapPt1, leapPt2, leapVal);
Vector3 gameObjPos = Vector3.Lerp (gameObjPt1, gameObjPt2, leapValNormalized);
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
5 People are following this question.
using Contains(gameObject) to find and destroy a gameObject from a list
'public GameObject ball' or 'public Ball ball'?
Switching between two objects using "enable"?
Game crashes on start due to large gameObject
Touch are laggy on ipod touch(4th gen.)