Home > Blockchain >  Change gain of hand tracking in Unity
Change gain of hand tracking in Unity

Time:11-03

I would like to change the gain of the hand tracking in Unity but I have some problems when I try to move the hand. Indeed, when I try to move the right hand, there is an error telling me the position of the hand is infinity. Do you have an idea about that ? I saw lots of things in forums but I don't understand what is the problem.

Here is my code :

    // Variables
    public GameObject hand;
    public GameObject handRenderer;
    private Vector3 posOrigin;

    // Settings
    public float gain;

    // Start is called before the first frame update
    void Start()
    {
        Vector3 posOrigin = handRenderer.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        float dx = transfo(handRenderer.transform.position.x, posOrigin.x);
        float dz = transfo(handRenderer.transform.position.z, posOrigin.z);
        hand.transform.position = new Vector3(posOrigin.x   dx, 0, posOrigin.z   dz);
    }

    float transfo ( float origin, float actual )
    {
        return gain * (actual - origin);
    }

The hand renderer is the point I take for my calcul because it does not work with the object RightHand directly. Thanks a lot

CodePudding user response:

First of all in Start in

Vector3 posOrigin = handRenderer.transform.position;

you are creating a new local variable.

You rather wanted to assign your class field

void Start()
{
    posOrigin = handRenderer.transform.position;
}

and then the transfo seems to be used inverted .. it should always be

current - original

but you are passing in first the current one and then the original which results in

original - current

I think in general you should rather do this in a single step and instead of the method rather use e.g.

var delta = handRenderer.transform.position - posOrigin;
var sizedDelta = delta * gain;
sizedDelta.y = 0;
hand.transform.position = posOrigin   sizedDelta;

Then make sure you haven't by accident the same object referenced in hand and handRenderer or that handRenderer isn't somewhere nested under hand.

Otherwise whenever you move the hand in your Update you also move the handRenderer the calculation is based on => Example

  • Init at 0,0,0 with gain = 2
  • moves handRenderer to 1,1,1
  • => moves hand to 2,2,2
  • moves hanRenderer along to now 3,3,3
  • => next frame moves hand to 6,6,6
  • mves handRenderer along to now 9, 9, 9
  • => next frame moves hand to 18,18,18
  • .....
  • Related