Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UdonSharp;
- using UnityEngine;
- using VRC.SDKBase;
- using VRC.Udon;
- namespace DrBlackRat
- {
- [UdonBehaviourSyncMode(BehaviourSyncMode.None)]
- public class AvatarHeightController : UdonSharpBehaviour
- {
- [Tooltip("Allow Users to change their Avatars Eye Height between the May & Min Height")]
- public bool allowScaling = true;
- [Tooltip("Enfore Min & Max Height, without this someone could still use an Avatar which is bigger / smaller than the Max / Min Height by default")]
- public bool enforceMinMaxHeight = false;
- [Space]
- [Tooltip("Maximum Avatar Eye Height in Meters")]
- public float maxHeight = 5f;
- [Tooltip("Minimum Avatar Eye Height in Meters")]
- public float minHeight = 0.2f;
- public override void OnPlayerJoined(VRCPlayerApi player)
- {
- if (player.isLocal)
- {
- Networking.LocalPlayer.SetManualAvatarScalingAllowed(allowScaling);
- Networking.LocalPlayer.SetAvatarEyeHeightMaximumByMeters(maxHeight);
- Networking.LocalPlayer.SetAvatarEyeHeightMinimumByMeters(minHeight);
- }
- }
- public void OnAvatarEyeHeightChanged(VRCPlayerApi playerApi, float height)
- {
- if (playerApi.isLocal && enforceMinMaxHeight)
- {
- _enforceHeight();
- }
- }
- public void OnAvatarChanged(VRCPlayerApi playerApi)
- {
- if (playerApi.isLocal && enforceMinMaxHeight)
- {
- _enforceHeight();
- }
- }
- private void _enforceHeight()
- {
- float height = Networking.LocalPlayer.GetAvatarEyeHeightAsMeters();
- Debug.Log($"[Avatar Height Controller] Current Height: {height}");
- if (height > maxHeight)
- {
- Networking.LocalPlayer.SetAvatarEyeHeightByMeters(maxHeight);
- Debug.Log($"[Avatar Height Controller] You are too tall! New Height: {maxHeight}");
- SendCustomEventDelayedSeconds("_enableManualScaling", 5);
- }
- if (height < minHeight)
- {
- Networking.LocalPlayer.SetAvatarEyeHeightByMeters(minHeight);
- Debug.Log($"[Avatar Height Controller] You are too short! New Height: {minHeight}");
- SendCustomEventDelayedSeconds("_enableManualScaling", 5);
- }
- }
- public void _enableManualScaling()
- {
- Networking.LocalPlayer.SetManualAvatarScalingAllowed(allowScaling);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment