Search Unity

Access or calculate the degree of bend in a bone?

Discussion in 'Animation' started by alexchesser, Feb 1, 2020.

  1. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    Hi There,

    I'm looking to work with a humanoid model and to script some events based on the degree of the bend in a given bone. So for example, when fingers or knees are at MAX BEND I'd like to run some VFX. I can think of a few ways I would do this conceptually, but I'm wondering if anyone has experience in this space.

    method 1: *somehow* access the mechanim avatar / rig and check its contraints at runtime. Calculate a percentage rotation based on what the rig says its absolute bounds are.

    Do do that, I'd have to be able to figure out how to access the AVATAR -> per-muscle settings at runtime and calculate a value based on its current position. A percentage could be calculated with a little bit of math there.

    upload_2020-2-1_14-1-29.png

    method 2: store the bone's LOCAL ROTATION in the component in the Start method. Calculate the amount the joint has rotated from the starting position (somehow), maybe use a simple heuristic of 90 degrees from start as a maximized bend.

    Probably good enough for faking it and a bit simpler overall.

    I guess for this second method - I'd need to know how to calculate the difference between two quaternions. I suppose I could google for that.

    Anyone have any thoughts or experience on this?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,568
    Pretty sure method 1 isn't possible because we don't have access to the necessary internals.

    But method 2 should be easy using Quaternion.Angle.
     
    alexchesser likes this.
  3. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    It was actually super easy! You can even make the "great than angle" a serialized field so it works on a per joint basis.