Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to avoid lookat stacking?

Discussion in 'Scripting' started by Kyrieru, Jul 26, 2020.

  1. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    I want to make it so the body and head all look at the player with different weights.

    However, because execution is arbitrary, they all occur out of order, causing things to look too far.
    What are some methods to avoid this? The only methods I can think of all require adding stuff outside of the script.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Usually that's all done by a single script/function which has a handle on all of the bones involved, and it's not necessarily simple to figure out how each bone should be rotated.

    Are you doing this on a rigged model? Probably the easiest thing would be to use
    Animator.SetLookAtPosition along with Animator.SetLookAtWeight, but that assumes your model is in a certain format:

    https://docs.unity3d.com/ScriptReference/Animator.SetLookAtWeight.html
     
    Last edited: Jul 26, 2020
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I'm a newbie at this animation stuff but would inverse kinematics be the way to go here? Like a working backwards solution.
     
  4. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    I think it sounds like I have to make a script that has a list of everything that needs to look, and then run them in order from a controller.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Not really. At least, not if you're just going to be using Transform.LookAt. You need for each of the "bones" in the chain rotates only a portion of the total required to get the tail to be looking at the right place. As @PraetorBlue mentioned, there's a name for that: Inverse Kinematics. It's not a trivial thing to write on your own. There are plenty of packages on the asset store that offer this, but Unity also has some limited, built-in IK for some cases.

    What does your model look like? Does it have a formal Rig?
     
  6. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    Ah, I can see where what I said is confusing.

    The body sections and the head are all individually looking at the target. So if the target is above the character,
    the lower body would face the target 10%, the chest rotates about 25%, and the head rotates 100%. This also means that if something is right in front of the character, they will essentially lean back while looking at it.

    However because each section needs to individually look, they must be done in order, otherwise the head might rotate first, and then the chest, which causes the head to look the wrong way.
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    But still, not quite, unless these are all completely independently, non-nested objects. Usually, the Chest will be a child object of the body, and the head will be a child object of the chest. So if you rotate the lower body by 10%, that implicitly rotates the chest and head by that amount, since they're child objects. So if you apply 100% of the rotation to the head, it would be over-rotated when you consider the rotation it inherits from the parent objects. IK solvers are designed to account for the cumulative rotation. But maybe by "100%" you mean 100% of the rest of the way. In any case, I don't think LookAt works with partial amounts, so you'd end up needing to do some semi-complicated rotation logic to get a rotation that rotates a partial amount, based on the weight, between two rotations.

    Again, what does your model look like? Do you have an Animator on it? Can you just use the built-in IK to set SetLookAtPosition on the animator, to avoid having to reinvent the wheel?
     
  8. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    I mean 25% as in weight.
    0% would be rotation from parent bone, 100% would be looking directly at target 50% is halfway.
    If lookat functions were all run in order from root to head, then they would not stack.

    There is nothing special about the model, it is just a basic human body structure.
     
  9. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
  10. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
  11. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    If you animate the "weight" value over time (the first parameter to SetLookAtWeight), it should weight in smoothly/gradually.
     
  12. Kyrieru

    Kyrieru

    Joined:
    Mar 2, 2015
    Posts:
    42
    That sounds soul draining....

    Well, I ended up finding out what to do anyhow. Controller script that runs the method of each bone in order by adding them to a list.