PickupSpeedLimiter.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDK3.Components;
  4. using VRC.SDKBase;
  5. [UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
  6. [DisallowMultipleComponent]
  7. [AddComponentMenu("Artyom Scripting System/Pickup Speed Limiter")]
  8. public class PickupSpeedLimiter : UdonSharpBehaviour
  9. {
  10. public float maxSpeed = 5.0f; // Maximum allowed speed for the pickup
  11. public bool debugSpeed;
  12. private VRC_Pickup pickup; // Reference to the VRC_Pickup component
  13. private Rigidbody rb;
  14. private bool synced; // Has VRCObjectSync: only its owner limits it
  15. void Start()
  16. {
  17. pickup = GetComponent<VRC_Pickup>();
  18. rb = GetComponent<Rigidbody>();
  19. synced = GetComponent<VRCObjectSync>() != null;
  20. if (pickup == null || rb == null)
  21. {
  22. Debug.LogError("[AUMSS] Pickup Speed Limiter: needs a VRC_Pickup and a Rigidbody on this GameObject.");
  23. enabled = false;
  24. }
  25. }
  26. // FixedUpdate: once per physics step, so no step runs over the limit
  27. void FixedUpdate()
  28. {
  29. if (pickup == null || rb == null) return; // Not set up (enabled = false only takes effect next frame)
  30. if (pickup.IsHeld) return;
  31. if (synced && !Networking.IsOwner(gameObject)) return; // Anyone else would fight the sync
  32. Vector3 velocity = rb.velocity;
  33. if (velocity.sqrMagnitude > maxSpeed * maxSpeed)
  34. {
  35. if (debugSpeed) Debug.LogWarning("[AUMSS] Pickup Speed Limiter: " + gameObject.name + " at " + velocity.magnitude.ToString() + ", limited to " + maxSpeed.ToString());
  36. rb.velocity = velocity.normalized * maxSpeed;
  37. }
  38. }
  39. }