PickupSpeedLimiter.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UdonSharp;
  2. using UnityEngine;
  3. using VRC.SDKBase;
  4. using VRC.Udon;
  5. using System;
  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. void Start()
  14. {
  15. pickup = GetComponent<VRC_Pickup>();
  16. if (pickup == null)
  17. {
  18. Debug.LogError("No VRC_Pickup component found on this GameObject.");
  19. }
  20. }
  21. void Update()
  22. {
  23. if (pickup != null && !(pickup.IsHeld))
  24. {
  25. Rigidbody rb = pickup.GetComponent<Rigidbody>();
  26. if (rb != null)
  27. {
  28. Vector3 velocity = rb.velocity;
  29. if (debugSpeed) {
  30. if (velocity.magnitude > maxSpeed) { Debug.LogWarning("Speed of cube: " + rb.velocity.magnitude.ToString());}
  31. Debug.Log("Speed of cube: " + rb.velocity.magnitude.ToString()); }
  32. if (velocity.magnitude > maxSpeed)
  33. {
  34. rb.velocity = velocity.normalized * maxSpeed;
  35. }
  36. }
  37. }
  38. }
  39. }