Johnny Robert
Johnny Robert
CC#
Created by Johnny Robert on 2/15/2023 in #help
❔ Local Space Clamping Values Issues
Only the minimum clamping values seem to work correctly, the maximum clamping values are catapulting my eyes away from the eye socket, check video attached.
using UnityEngine;

public class EyeControl : MonoBehaviour
{
public Transform headBone;
public Transform leftEyeBone;
public Transform rightEyeBone;
public Transform leftEye;
public Transform rightEye;
public Transform target;

public float eyeOffset = 0.1f;
public float maxLookDistance = 10.0f;

void Update()
{
// get the head bone position and rotation in world space
Vector3 headPosition = headBone.position;
Quaternion headRotation = headBone.rotation;

// calculate the eye positions based on the target object position
Vector3 targetPosition = target.position;
Vector3 leftEyePosition = targetPosition + (headRotation * Vector3.left * eyeOffset);
Vector3 rightEyePosition = targetPosition + (headRotation * Vector3.right * eyeOffset);

// Lock the Z position of the eye bones
leftEyePosition.z = leftEyeBone.position.z;
rightEyePosition.z = rightEyeBone.position.z;

// convert world space eye positions to local positions
Vector3 leftEyeLocalPosition = leftEyeBone.InverseTransformPoint(leftEyePosition);
Vector3 rightEyeLocalPosition = rightEyeBone.InverseTransformPoint(rightEyePosition);

// set the local positions of the eye bones
leftEyeBone.localPosition = leftEyeLocalPosition;
rightEyeBone.localPosition = rightEyeLocalPosition;

// clamp the position of the left eye bone
Vector3 clampedPosition = leftEyeBone.localPosition;
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -0.0102f, -0.0539f);
clampedPosition.y = Mathf.Clamp(clampedPosition.y, 0.0567f, 0.073f);
clampedPosition.z = Mathf.Clamp(clampedPosition.z, 0.0946f, 0.0716f);
leftEyeBone.localPosition = clampedPosition;
}
}
using UnityEngine;

public class EyeControl : MonoBehaviour
{
public Transform headBone;
public Transform leftEyeBone;
public Transform rightEyeBone;
public Transform leftEye;
public Transform rightEye;
public Transform target;

public float eyeOffset = 0.1f;
public float maxLookDistance = 10.0f;

void Update()
{
// get the head bone position and rotation in world space
Vector3 headPosition = headBone.position;
Quaternion headRotation = headBone.rotation;

// calculate the eye positions based on the target object position
Vector3 targetPosition = target.position;
Vector3 leftEyePosition = targetPosition + (headRotation * Vector3.left * eyeOffset);
Vector3 rightEyePosition = targetPosition + (headRotation * Vector3.right * eyeOffset);

// Lock the Z position of the eye bones
leftEyePosition.z = leftEyeBone.position.z;
rightEyePosition.z = rightEyeBone.position.z;

// convert world space eye positions to local positions
Vector3 leftEyeLocalPosition = leftEyeBone.InverseTransformPoint(leftEyePosition);
Vector3 rightEyeLocalPosition = rightEyeBone.InverseTransformPoint(rightEyePosition);

// set the local positions of the eye bones
leftEyeBone.localPosition = leftEyeLocalPosition;
rightEyeBone.localPosition = rightEyeLocalPosition;

// clamp the position of the left eye bone
Vector3 clampedPosition = leftEyeBone.localPosition;
clampedPosition.x = Mathf.Clamp(clampedPosition.x, -0.0102f, -0.0539f);
clampedPosition.y = Mathf.Clamp(clampedPosition.y, 0.0567f, 0.073f);
clampedPosition.z = Mathf.Clamp(clampedPosition.z, 0.0946f, 0.0716f);
leftEyeBone.localPosition = clampedPosition;
}
}
3 replies