JumpPad.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Remote players get launched by their own client, and speed/velocity calls on them throw and halt this script
  50. if (!colidedPlayer.isLocal)
  51. {
  52. SendCustomEventDelayedSeconds(nameof(EndLaunchAnim), arcTime-0.2f);
  53. return;
  54. }
  55. player = colidedPlayer;
  56. // Get speeds to reinstate after jump imobilization
  57. defaultRun = player.GetRunSpeed();
  58. defaultStrafe = player.GetStrafeSpeed();
  59. defaultWalk = player.GetWalkSpeed();
  60. // Set current velocity to triggered player and limit their movement
  61. LimitMobility();
  62. player.SetVelocity(appliedVelocity);
  63. if (debug) Debug.Log("Jump Pad Controller: Player Velocity Set to:"+ appliedVelocity.ToString());
  64. SendCustomEventDelayedSeconds(nameof(Mobilize), arcTime-0.2f);
  65. }
  66. private void OnTriggerEnter(Collider other)
  67. {
  68. appliedVelocity = CalculateInitialVelocity(self.position, jumpTarget.position, -9.8f, arcTime);
  69. VRC_Pickup pickup = other.GetComponent<VRC_Pickup>();
  70. Rigidbody body = other.GetComponent<Rigidbody>();
  71. if (pickup && body)
  72. {
  73. pickup.Drop();
  74. body.velocity = appliedVelocity;
  75. }
  76. }
  77. public void LimitMobility()
  78. {
  79. player.SetWalkSpeed(walkLimit);
  80. player.SetRunSpeed(runLimit);
  81. player.SetStrafeSpeed(strafeLimit);
  82. if (debug) Debug.Log("Jump Pad Controller: Limited Player Mobility for " + arcTime.ToString() + " seconds");
  83. }
  84. public void Mobilize()
  85. {
  86. player.SetWalkSpeed(defaultWalk);
  87. player.SetRunSpeed(defaultRun);
  88. player.SetStrafeSpeed(defaultStrafe);
  89. if (debug) Debug.Log("Jump Pad Controller: De-Limited Player Mobility after" + arcTime.ToString() + " seconds");
  90. EndLaunchAnim();
  91. }
  92. public void EndLaunchAnim()
  93. {
  94. launchAnim.SetBool(animName,false);
  95. if (debug) Debug.Log("Jump Pad Controller: Animator State attempted to be set to False");
  96. }
  97. }