Guest User

Untitled

a guest
Jun 24th, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1.  
  2. using UdonSharp;
  3. using UnityEngine;
  4. using VRC.SDKBase;
  5. using VRC.Udon;
  6.  
  7. namespace DrBlackRat
  8. {
  9.     [UdonBehaviourSyncMode(BehaviourSyncMode.None)]
  10.     public class AvatarHeightController : UdonSharpBehaviour
  11.     {
  12.         [Tooltip("Allow Users to change their Avatars Eye Height between the May & Min Height")]
  13.         public bool allowScaling = true;
  14.         [Tooltip("Enfore Min & Max Height, without this someone could still use an Avatar which is bigger / smaller than the Max / Min Height by default")]
  15.         public bool enforceMinMaxHeight = false;
  16.         [Space]
  17.         [Tooltip("Maximum Avatar Eye Height in Meters")]
  18.         public float maxHeight = 5f;
  19.         [Tooltip("Minimum Avatar Eye Height in Meters")]
  20.         public float minHeight = 0.2f;
  21.  
  22.         public override void OnPlayerJoined(VRCPlayerApi player)
  23.         {
  24.             if (player.isLocal)
  25.             {
  26.                 Networking.LocalPlayer.SetManualAvatarScalingAllowed(allowScaling);
  27.                 Networking.LocalPlayer.SetAvatarEyeHeightMaximumByMeters(maxHeight);
  28.                 Networking.LocalPlayer.SetAvatarEyeHeightMinimumByMeters(minHeight);
  29.             }
  30.         }
  31.         public void OnAvatarEyeHeightChanged(VRCPlayerApi playerApi, float height)
  32.         {
  33.             if (playerApi.isLocal && enforceMinMaxHeight)
  34.             {
  35.                 _enforceHeight();
  36.             }
  37.         }
  38.         public void OnAvatarChanged(VRCPlayerApi playerApi)
  39.         {
  40.             if (playerApi.isLocal && enforceMinMaxHeight)
  41.             {
  42.                 _enforceHeight();
  43.             }
  44.         }
  45.         private void _enforceHeight()
  46.         {
  47.             float height = Networking.LocalPlayer.GetAvatarEyeHeightAsMeters();
  48.  
  49.             Debug.Log($"[Avatar Height Controller] Current Height: {height}");
  50.             if (height > maxHeight)
  51.             {
  52.                 Networking.LocalPlayer.SetAvatarEyeHeightByMeters(maxHeight);
  53.                 Debug.Log($"[Avatar Height Controller] You are too tall! New Height: {maxHeight}");
  54.                 SendCustomEventDelayedSeconds("_enableManualScaling", 5);
  55.             }
  56.             if (height < minHeight)
  57.             {
  58.                 Networking.LocalPlayer.SetAvatarEyeHeightByMeters(minHeight);
  59.                 Debug.Log($"[Avatar Height Controller] You are too short! New Height: {minHeight}");
  60.                 SendCustomEventDelayedSeconds("_enableManualScaling", 5);
  61.             }
  62.         }
  63.         public void _enableManualScaling()
  64.         {
  65.             Networking.LocalPlayer.SetManualAvatarScalingAllowed(allowScaling);
  66.         }
  67.     }
  68. }
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment