- Home /
Trying to get separate x and y angles for eye look using blend shapes
Hello!
I've been doing pretty well muddling my way though coding, but I can't seem to figure this out.
I want to use blendshapes for eye look, there are 4 for each eye, one for each direction. I'm trying to get separate X and Y values, with negative values for left and down. I thought I had figured it out with the code below, but the numbers I'm getting back make no sense.
Vector3 relativePosition = lookTarget.position - blendshapeEyeMesh.transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePosition);
Quaternion current = transform.localRotation;
float eyeLookX = rotation.eulerAngles.x - transform.localRotation.eulerAngles.x;
float eyeLookY = rotation.eulerAngles.y - transform.localRotation.eulerAngles.y;
//take x and y values, divide by the max value and discard values larger than one
//set appropriate blend shapes based on positive or negative value
Is there a function I can use to get a float for the angle for Y and X separately? Or will I have to do something more complicated?
Answer by phyzix101 · Nov 02, 2021 at 10:07 AM
Wow, silly me. Not sure why I thought to use localRotation!
Vector3 relativePosition = lookTarget.position - blendshapeEyeMesh.transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePosition);
Quaternion current = transform.rotation;
float eyeLookX = rotation.eulerAngles.x - current.eulerAngles.x;
float eyeLookY = rotation.eulerAngles.y - current.eulerAngles.y;
That is getting me numbers closer to what I want. All I need to do now is to make the logic to figure out which number to add/subtract from which, so I don't get small numbers subtracting from 360.
Edit: On looking close the numbers were still a bit odd. So then I found FromToRotation! That worked great, and the logic to do the directions was pretty simple as well. It even works if the character is not upright! Which I thought might involve a lot of code to work out.
Quaternion lookDirection = Quaternion.FromToRotation(relativePosition, blendshapeEyeMesh.transform.forward);
float eyeLookX = lookDirection.eulerAngles.x;
float eyeLookY = lookDirection.eulerAngles.y;
if (eyeLookX > 180)
{
float x = +Mathf.Clamp((360 - eyeLookX) / 45 * 100, 0, 100);
blendshapeEyeMesh.SetBlendShapeWeight(leftEyeUpID, x);
blendshapeEyeMesh.SetBlendShapeWeight(rightEyeUpID, x);
}
else
{
float x = Mathf.Clamp(eyeLookX / 45 * 100, 0, 100);
blendshapeEyeMesh.SetBlendShapeWeight(leftEyeDownID, x);
blendshapeEyeMesh.SetBlendShapeWeight(rightEyeDownID, x);
}
if (eyeLookY > 180)
{
float x = Mathf.Clamp((360 - eyeLookY) / 45 * 100, 0, 100);
blendshapeEyeMesh.SetBlendShapeWeight(leftEyeRightID, x);
blendshapeEyeMesh.SetBlendShapeWeight(rightEyeRightID, x);
}
else
{
float x = Mathf.Clamp(eyeLookY / 45 * 100, 0, 100);
blendshapeEyeMesh.SetBlendShapeWeight(leftEyeLeftID, x);
blendshapeEyeMesh.SetBlendShapeWeight(rightEyeLeftID, x);
}
I'll probably have to zero out the opposite directions as well, but I think I will have to make a function to control eyelids along with the eyes and add that in there. I'd be interested in knowing if there is anything else I could do better here.
Your answer
Follow this Question
Related Questions
Change a property based on angle? 1 Answer
Brand new to Unity, trying to grasp some basic stuff related to cameras and angles 1 Answer
Calculating angles to a target and turning towards it 1 Answer
Why is rotation locking to an axis? 3 Answers
2D Ball Bouncing Angle Problem (Without Rigidbody) 0 Answers