How to do it...

To make characters follow an object with their gaze, follow these steps:

  1. Import the character into Unity and place it in a scene.
  2. Make sure to create an Animator Controller with at least one animation, or use an existing one.
  3. To use the firstQuaternion.LookRotation() method, create a new script and call it CharacterLookAt.cs.
  4. In that script, we use the void LateUpdate() function to alter bone rotation after all animations are evaluated. In that function, we first check if a publicfloat weight variable is less than or equal to 0. If so, we don't do anything (we turn off the look at behavior):
        if (weight <= 0f) 
        { 
            return; 
        } 
  1. If that is not true, we calculate our desired lookDirection. This is a vector in which our character should look. We calculate it by subtracting our public Transform bone position from the public Transform target position. This vector is then damped in time using the SmoothDamp() function. This prevents it from sudden changes. We use a public floatdampTime variable to determine the time in which we smooth the vector out. The dampVelocity vector is class member variable required by the SmoothDamp() function to store the changes in the lookDirection vector between frames:
        lookDirection = Vector3.SmoothDamp(lookDirection, 
        target.position - bone.position, ref dampVelocity, 
        dampTime); 
  1. Next we check if the angle between our desired lookDirection vector and the character's transform.forward vector is greater than our public float maxAngle value. If so, we calculate the finalLookVector. This is our character's transform.forward vector, which is rotated toward the desired lookDirection vector by the maxAngle degrees. This way we create a cone of vision for our character and prevent it from breaking the neck joint. We need to use the MathfDeg2Rad constant to change our maxAngle degrees to radians because the Vector3.RotateTowards() function uses radians instead of degrees. If the angle between lookDirection and transform.forward is less than or equal to maxAngle, we don't alter the lookDirection:
        if (Vector3.Angle(lookDirection, transform.forward) > 
        maxAngle) 
        { 
            finalLookVector = 
            Vector3.RotateTowards(transform.forward, 
            lookDirection, Mathf.Deg2Rad*maxAngle, 0.5f); 
        } 
        else 
        { 
            finalLookVector = lookDirection; 
        } 
  1. Finally, we calculate the Quaternion rotation value by using the Quaternion.LookRotation() method and multiplying its result by a public Vector3 additinalRotation value. First we need to turn this Vector3 into a Quaternion by using the Quaternion.Euler() function. Multiplying two quaternions is simply adding an additional rotation. We need to use the additionalRotation vector because, in most cases, the head bone's forward axis doesn't match the face of our character. By applying an additional rotation of +90 or -90 degrees in one of the axes (XY, or Z), we can make the script work for every rig. After calculating the rotation, we linearly interpolate the current bone.rotation value to our calculated rotation using the weight value. This way we can turn the look at on and off easily:
        rotation = Quaternion.LookRotation(finalLookVector) * 
        Quaternion.Euler(additionalRotation); 
        bone.rotation = Quaternion.Lerp(bone.rotation, rotation, 
        weight); 
  1. Save the script and add it to the character.
  2. Drag and drop the head bone of the character to the Bone field in the script's Inspector.
  3. Drag and drop the look at target transform to the Target field in the script's Inspector.
  4. You may need to adjust the Additional Rotation field. Experiment in Play Mode with +90 or -90 values in different axes to find a matching value. Modifying one axis at a time should be enough.
  5. Move the target transform in Play Mode to see the effect (you can do it in the Scene View).
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.38.176