ambrose01

SekiroGrapple.cs

Jan 1st, 2026 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SekiroGrapple : MonoBehaviour
  5. {
  6. [Header("Settings")]
  7. public float maxCheckDistance = 50f;
  8. public float minGrappleDistance = 4f;
  9. public float grappleSpeed = 15f;
  10. public LayerMask grappleLayer;
  11.  
  12. [Header("Visuals")]
  13. public LineRenderer ropeRenderer;
  14. public Transform handPos;
  15. public float grappleFOV = 90f;
  16. public float fovChangeSpeed = 5f;
  17.  
  18. [Header("References")]
  19. public Transform playerCamera;
  20. public Transform playerBody;
  21. public PlayerController playerMovement;
  22.  
  23. private GrapplePoint currentTarget;
  24. private bool isGrappling = false;
  25. private float defaultFOV;
  26. private Camera cam;
  27.  
  28. void Start()
  29. {
  30. ropeRenderer.enabled = false;
  31. cam = playerCamera.GetComponent<Camera>();
  32. defaultFOV = cam.fieldOfView;
  33. }
  34.  
  35. void Update()
  36. {
  37. HandleFOV();
  38.  
  39. if (isGrappling) return;
  40.  
  41. ScanForGrapplePoint();
  42.  
  43. if (Input.GetKeyDown(KeyCode.E) && currentTarget != null)
  44. {
  45. float dist = Vector3.Distance(playerBody.position, currentTarget.transform.position);
  46.  
  47. if (dist > currentTarget.grappleRange)
  48. {
  49. return;
  50. }
  51.  
  52. if (dist < minGrappleDistance)
  53. {
  54. return;
  55. }
  56.  
  57. StartCoroutine(GrappleToPoint(currentTarget.transform.position));
  58. }
  59. }
  60.  
  61. void ScanForGrapplePoint()
  62. {
  63. RaycastHit hit;
  64. bool hitSomething = Physics.SphereCast(playerCamera.position, 0.2f, playerCamera.forward, out hit, maxCheckDistance, grappleLayer);
  65.  
  66. if (hitSomething)
  67. {
  68. GrapplePoint point = hit.collider.GetComponent<GrapplePoint>();
  69.  
  70. if (point != null)
  71. {
  72. currentTarget = point;
  73. point.isTargeted = true;
  74. }
  75. else
  76. {
  77. currentTarget = null;
  78. }
  79. }
  80. else
  81. {
  82. currentTarget = null;
  83. }
  84. }
  85.  
  86. void HandleFOV()
  87. {
  88. float targetFOV = isGrappling ? grappleFOV : defaultFOV;
  89. cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, targetFOV, Time.deltaTime * fovChangeSpeed);
  90. }
  91.  
  92. IEnumerator GrappleToPoint(Vector3 targetPosition)
  93. {
  94. isGrappling = true;
  95. if(playerMovement != null) playerMovement.canMove = false;
  96. ropeRenderer.enabled = true;
  97.  
  98. float t = 0;
  99. Vector3 startPos = playerBody.position;
  100. float distance = Vector3.Distance(startPos, targetPosition);
  101.  
  102. while (t < 1)
  103. {
  104. t += Time.deltaTime * grappleSpeed / distance;
  105. playerBody.position = Vector3.Lerp(startPos, targetPosition, t);
  106.  
  107. if (handPos != null) ropeRenderer.SetPosition(0, handPos.position);
  108. ropeRenderer.SetPosition(1, targetPosition);
  109.  
  110. yield return null;
  111. }
  112.  
  113. ropeRenderer.enabled = false;
  114. isGrappling = false;
  115. if(playerMovement != null) playerMovement.canMove = true;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment