Advertisement
Guest User

BlinkEyeTracking.cs

a guest
Jul 13th, 2019
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BlinkEyeTracking : MonoBehaviour
  6. {
  7.     SkinnedMeshRenderer rendererFace;
  8.     [SerializeField]
  9.     private AnimationCurve animationCurve;
  10.     [SerializeField]
  11.     int BlendShapeIndex_Eyes_Horizontal;
  12.     [SerializeField]
  13.     int BlendShapeIndex_Eyes_Vertical;
  14.  
  15.     float positionx;
  16.     float positiony;
  17.  
  18.     Camera mainCam;
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         rendererFace = GetComponent<SkinnedMeshRenderer>();
  23.         mainCam = Camera.main;
  24.     }
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         if (Input.GetKeyDown(KeyCode.B))
  30.         {        
  31.             StartCoroutine(Blink(0.3f));
  32.         }
  33.  
  34.         positiony = Mathf.Clamp((mainCam.ScreenToViewportPoint(Input.mousePosition).y - 0.5f) * 100, -100, 100);
  35.         positionx = Mathf.Clamp((mainCam.ScreenToViewportPoint(Input.mousePosition).x - 0.5f) * -100, -100, 100);
  36.         rendererFace.SetBlendShapeWeight(BlendShapeIndex_Eyes_Vertical, positiony);
  37.         rendererFace.SetBlendShapeWeight(BlendShapeIndex_Eyes_Horizontal, positionx);
  38.     }
  39.  
  40.  
  41.     IEnumerator Blink(float duration)
  42.     {
  43.         float time = 0f;
  44.         while (time <= duration)
  45.         {
  46.             time = time + Time.deltaTime;
  47.             float percent = Mathf.Clamp01(time / duration);
  48.             rendererFace.SetBlendShapeWeight(0, (animationCurve.Evaluate(percent) * 100));
  49.             yield return null;
  50.         }
  51.     }
  52.  
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement