Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SekiroGrapple : MonoBehaviour
- {
- [Header("Settings")]
- public float maxCheckDistance = 50f;
- public float minGrappleDistance = 4f;
- public float grappleSpeed = 15f;
- public LayerMask grappleLayer;
- [Header("Visuals")]
- public LineRenderer ropeRenderer;
- public Transform handPos;
- public float grappleFOV = 90f;
- public float fovChangeSpeed = 5f;
- [Header("References")]
- public Transform playerCamera;
- public Transform playerBody;
- public PlayerController playerMovement;
- private GrapplePoint currentTarget;
- private bool isGrappling = false;
- private float defaultFOV;
- private Camera cam;
- void Start()
- {
- ropeRenderer.enabled = false;
- cam = playerCamera.GetComponent<Camera>();
- defaultFOV = cam.fieldOfView;
- }
- void Update()
- {
- HandleFOV();
- if (isGrappling) return;
- ScanForGrapplePoint();
- if (Input.GetKeyDown(KeyCode.E) && currentTarget != null)
- {
- float dist = Vector3.Distance(playerBody.position, currentTarget.transform.position);
- if (dist > currentTarget.grappleRange)
- {
- return;
- }
- if (dist < minGrappleDistance)
- {
- return;
- }
- StartCoroutine(GrappleToPoint(currentTarget.transform.position));
- }
- }
- void ScanForGrapplePoint()
- {
- RaycastHit hit;
- bool hitSomething = Physics.SphereCast(playerCamera.position, 0.2f, playerCamera.forward, out hit, maxCheckDistance, grappleLayer);
- if (hitSomething)
- {
- GrapplePoint point = hit.collider.GetComponent<GrapplePoint>();
- if (point != null)
- {
- currentTarget = point;
- point.isTargeted = true;
- }
- else
- {
- currentTarget = null;
- }
- }
- else
- {
- currentTarget = null;
- }
- }
- void HandleFOV()
- {
- float targetFOV = isGrappling ? grappleFOV : defaultFOV;
- cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, targetFOV, Time.deltaTime * fovChangeSpeed);
- }
- IEnumerator GrappleToPoint(Vector3 targetPosition)
- {
- isGrappling = true;
- if(playerMovement != null) playerMovement.canMove = false;
- ropeRenderer.enabled = true;
- float t = 0;
- Vector3 startPos = playerBody.position;
- float distance = Vector3.Distance(startPos, targetPosition);
- while (t < 1)
- {
- t += Time.deltaTime * grappleSpeed / distance;
- playerBody.position = Vector3.Lerp(startPos, targetPosition, t);
- if (handPos != null) ropeRenderer.SetPosition(0, handPos.position);
- ropeRenderer.SetPosition(1, targetPosition);
- yield return null;
- }
- ropeRenderer.enabled = false;
- isGrappling = false;
- if(playerMovement != null) playerMovement.canMove = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment