SHARE
TWEET

Untitled

a guest Mar 25th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using RootMotion.FinalIK;
  2. using UnityEngine;
  3.  
  4. public class IkScaler : MonoBehaviour {
  5.  
  6.     public enum SizeModifiertype { Strech, Scale}
  7.  
  8.     [Header("General Fields")]
  9.     [SerializeField] VRIK ik;
  10.     [SerializeField] SizeModifiertype sizemodifier;
  11.     [SerializeField] float scaleDelayTimer;
  12.  
  13.     [Header("Apply Changes?")]
  14.     [SerializeField] bool changeArmLength;
  15.     [SerializeField] bool changeChestHeigth;
  16.  
  17.     [Header("ChestFields")]
  18.     [SerializeField] float minimumHeightDifference = 0.2f;
  19.     [SerializeField] float heightModifier;
  20.     [Header("StrechOnly")]
  21.     [SerializeField] Vector3 originalChestPosition;
  22.     [SerializeField] Vector3 heightDirection;
  23.  
  24.     [Header("Arms Fields Scale")]
  25.     [SerializeField] float _mlpModifier;
  26.     [Header("Arms Fields Strech")]
  27.     [SerializeField] bool changeUpperArm;
  28.     [SerializeField] bool changeForeArm;
  29.     [SerializeField] float scaleDistanceTreshHoldscaleDistanceTreshHold = 1.05f;
  30.     [SerializeField] float handModifier = 0.5f;
  31.  
  32.     float distanceModifier;
  33.     float heightDifference;
  34.     float playerHeight;
  35.     bool isRunningUserCheck;
  36.  
  37.     private void Awake()
  38.     {
  39.         if (ik == null)
  40.             ik = GetComponent<VRIK>();
  41.  
  42.         //Wear Headset
  43.         OVRManager.HMDMounted += DelayModifyBody;
  44.         //Take off
  45.         OVRManager.HMDUnmounted += HeadsetOff;
  46.  
  47.         InvokeRepeating("DelayModifyBody", 10f, 10f);
  48.     }
  49.  
  50.     private void LateUpdate()
  51.     {
  52.         if(sizemodifier == SizeModifiertype.Strech) ModifyHandPositions();
  53.     }
  54.  
  55.     void HeadsetOff()
  56.     {
  57.         Debug.Log("se saco el casco");
  58.     }
  59.  
  60.     void DelayModifyBody()
  61.     {
  62.         CancelInvoke("ModifyBody");
  63.         Invoke("ModifyBody", scaleDelayTimer);
  64.     }
  65.  
  66.     void ModifyBody()
  67.     {
  68.         if (OVRPlugin.userPresent)
  69.         {
  70.             switch (sizemodifier)
  71.             {
  72.                 case SizeModifiertype.Strech:
  73.                     if (changeChestHeigth) ChangeChestPosition();
  74.                     if (changeArmLength) ModifyHandDistance();
  75.                     break;
  76.  
  77.                 case SizeModifiertype.Scale:
  78.                     if (changeChestHeigth) ScaleBody();
  79.                     if (changeArmLength) ModifyHandDistance();
  80.                     break;
  81.  
  82.                 default:
  83.                     break;
  84.             }
  85.         }
  86.         else Debug.Log("user not present");
  87.     }
  88.  
  89.     void ChangeChestPosition()
  90.     {
  91.         //Debug.Log("ChestPosition");
  92.         heightDifference = (ik.solver.spine.headTarget.position.y - ik.references.root.position.y) / (ik.references.head.position.y - ik.references.root.position.y);
  93.         heightDifference -= 1;
  94.         if (Mathf.Abs(heightDifference) > minimumHeightDifference)
  95.         {
  96.             var newPos = originalChestPosition + heightDirection * heightDifference * heightModifier;
  97.             ik.references.chest.localPosition = newPos;
  98.             Debug.Log(newPos);
  99.         }
  100.     }
  101.  
  102.     void ScaleBody()
  103.     {
  104.         Debug.Log("BodyScale");
  105.         //Actual Scale / Model Scale
  106.         heightDifference = (ik.solver.spine.headTarget.position.y - ik.references.root.position.y) / (ik.references.head.position.y - ik.references.root.position.y);
  107.         ik.references.root.localScale = Vector3.one * heightDifference * heightModifier;
  108.     }
  109.    
  110.     void ModifyHandDistance()
  111.     {
  112.         switch (sizemodifier)
  113.         {
  114.             case SizeModifiertype.Strech:
  115.                 float sizeF = Vector3.Distance(ik.solver.rightArm.target.position, ik.references.root.position) / Vector3.Distance(ik.references.rightHand.position, ik.references.root.position);
  116.                 sizeF *= handModifier;
  117.                 if (sizeF > distanceModifier) distanceModifier = sizeF;
  118.                 break;
  119.  
  120.             //This should happen after the object got scaled
  121.             case SizeModifiertype.Scale:
  122.                 ik.solver.leftArm.armLengthMlp = 1 + transform.localScale.magnitude/2 * _mlpModifier;
  123.                 ik.solver.rightArm.armLengthMlp = 1 + transform.localScale.magnitude/2 * _mlpModifier;
  124.                 Debug.Log(1 + transform.localScale.magnitude / 2 * _mlpModifier);
  125.                 break;
  126.             default:
  127.                 break;
  128.         }
  129.     }
  130.  
  131.     void ModifyHandPositions()
  132.     {
  133.         var rigthUpperdistance = ik.references.rightForearm.position - ik.references.rightUpperArm.position;
  134.         var rigthLowerDistance = ik.references.rightHand.position - ik.references.rightForearm.position;
  135.         if (changeUpperArm) ik.references.rightUpperArm.position += rigthUpperdistance * distanceModifier;
  136.         if (changeForeArm) ik.references.rightForearm.position += rigthLowerDistance * distanceModifier;
  137.  
  138.         var leftUpperdistance = ik.references.leftForearm.position - ik.references.leftUpperArm.position;
  139.         var leftLowerDistance = ik.references.leftHand.position - ik.references.leftForearm.position;
  140.         if (changeUpperArm) ik.references.leftUpperArm.position += leftUpperdistance * distanceModifier;
  141.         if (changeForeArm) ik.references.leftForearm.position += leftLowerDistance * distanceModifier;
  142.     }
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top