JumpPad.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 
  2. using System.Numerics;
  3. using UdonSharp;
  4. using UnityEngine;
  5. using VRC.SDK3.Components;
  6. using VRC.SDKBase;
  7. using VRC.Udon;
  8. using Vector3 = UnityEngine.Vector3;
  9. [DisallowMultipleComponent]
  10. [AddComponentMenu("Artyom Scripting System/Jump Pad Controller")]
  11. public class JumpPad : UdonSharpBehaviour
  12. {
  13. public bool debug;
  14. public Animator launchAnim;
  15. public string animName = "isLaunch";
  16. public Transform jumpTarget;
  17. public Transform self;
  18. public float arcTime = 1.2f;
  19. public float walkLimit = 0.2f;
  20. public float runLimit = 0.4f;
  21. public float strafeLimit = 0.2f;
  22. private Vector3 appliedVelocity;
  23. private float defaultWalk;
  24. private float defaultStrafe;
  25. private float defaultRun;
  26. private VRCPlayerApi player;
  27. void Start()
  28. {
  29. launchAnim = GetComponent<Animator>();
  30. appliedVelocity = CalculateInitialVelocity(self.position, jumpTarget.position, -9.8f, arcTime);
  31. if (debug) Debug.Log("Jump Pad Controller: Velocity Calculated to:"+ appliedVelocity.ToString());
  32. }
  33. public static Vector3 CalculateInitialVelocity(Vector3 origin, Vector3 target, float gravity, float time)
  34. {
  35. // Compute the differences in position
  36. Vector3 displacement = target - origin;
  37. // Calculate the initial velocity components
  38. Vector3 initialVelocity = new Vector3(
  39. displacement.x / time, // Horizontal X velocity
  40. (displacement.y - 0.5f * gravity * time * time) / time, // Vertical Y velocity
  41. displacement.z / time // Horizontal Z velocity
  42. );
  43. return initialVelocity;
  44. }
  45. public override void OnPlayerTriggerEnter(VRCPlayerApi colidedPlayer) {
  46. appliedVelocity = CalculateInitialVelocity(self.position, jumpTarget.position, -9.8f, arcTime);
  47. launchAnim.SetBool(animName, true);
  48. if (debug) Debug.Log("Jump Pad Controller: Animator State attempted to be set to True");
  49. player = colidedPlayer;
  50. // Get speeds to reinstate after jump imobilization
  51. defaultRun = player.GetRunSpeed();
  52. defaultStrafe = player.GetStrafeSpeed();
  53. defaultWalk = player.GetWalkSpeed();
  54. // Set current velocity to triggered player and limit their movement
  55. LimitMobility();
  56. player.SetVelocity(appliedVelocity);
  57. if (debug) Debug.Log("Jump Pad Controller: Player Velocity Set to:"+ appliedVelocity.ToString());
  58. SendCustomEventDelayedSeconds(nameof(Mobilize), arcTime-0.2f);
  59. }
  60. private void OnTriggerEnter(Collider other)
  61. {
  62. appliedVelocity = CalculateInitialVelocity(self.position, jumpTarget.position, -9.8f, arcTime);
  63. VRC_Pickup pickup = other.GetComponent<VRC_Pickup>();
  64. Rigidbody body = other.GetComponent<Rigidbody>();
  65. if (pickup && body)
  66. {
  67. pickup.Drop();
  68. body.velocity = appliedVelocity;
  69. }
  70. }
  71. public void LimitMobility()
  72. {
  73. player.SetWalkSpeed(walkLimit);
  74. player.SetRunSpeed(runLimit);
  75. player.SetStrafeSpeed(strafeLimit);
  76. if (debug) Debug.Log("Jump Pad Controller: Limited Player Mobility for " + arcTime.ToString() + " seconds");
  77. }
  78. public void Mobilize()
  79. {
  80. player.SetWalkSpeed(defaultWalk);
  81. player.SetRunSpeed(defaultRun);
  82. player.SetStrafeSpeed(defaultStrafe);
  83. if (debug) Debug.Log("Jump Pad Controller: De-Limited Player Mobility after" + arcTime.ToString() + " seconds");
  84. launchAnim.SetBool(animName,false);
  85. if (debug) Debug.Log("Jump Pad Controller: Animator State attempted to be set to False");
  86. }
  87. }