Advertisement
Hyperspace_Wizard

Rimor_GrapplingHook

Jul 5th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GrapplingHook : MonoBehaviour
  6. {
  7.     [Header("References")]
  8.     public LayerMask whatIsGrappleAble;
  9.     public MovementController playerMovementController;
  10.     public Rigidbody2D playerBody;
  11.     public DistanceJoint2D joint;
  12.     public LineRenderer aimLine;
  13.     public LineRenderer ropeLine;
  14.     public Grapple grapple;
  15.  
  16.     [Header("Parameters")]
  17.     public float maxRange = 25f;
  18.  
  19.     [Header("Charges")]
  20.     public int maxCharges;
  21.     int currentCharges;
  22.     public float chargeTime;
  23.     float chargeTick;
  24.     public ManaOrb manaOrb;
  25.  
  26.     [Header("Info")]
  27.     public bool isGrappled = false;
  28.     public bool isFired = false;
  29.  
  30.     Vector2 aimDirection;
  31.  
  32.     [Header("SFX")]
  33.     public AudioClip fireSound;
  34.     public AudioClip attachSound;
  35.     public AudioClip retractSound;
  36.     public AudioClip rechargeSound;
  37.     public AudioClip falseFireSound;
  38.  
  39.     // Start is called before the first frame update
  40.     void Start()
  41.     {
  42.         grapple.transform.SetParent(null);
  43.         aimLine.positionCount = 2;
  44.         ropeLine.positionCount = 2;
  45.         joint.enabled = false;
  46.  
  47.         currentCharges = maxCharges;
  48.     }
  49.  
  50.     // Update is called once per frame
  51.     void Update()
  52.     {
  53.         aimDirection = ((Vector2)(Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position)).normalized;
  54.  
  55.         AimLineUpdate();
  56.         RopeLineUpdate();
  57.  
  58.         if(Input.GetButtonDown("Fire1"))
  59.         {
  60.             if (currentCharges > 0) FireGrapple();
  61.             else ExtensionMethods.PlayAudio(falseFireSound, 1.0f, Random.Range(0.8f, 1.2f));
  62.         }
  63.         if(Input.GetButtonUp("Fire1"))
  64.         {
  65.             UnGrapple();
  66.         }
  67.  
  68.         // Check if grapple out of range
  69.         if(!isGrappled && grapple.gameObject.activeSelf)
  70.         {
  71.             float distance = Vector2.Distance(transform.position, grapple.transform.position);
  72.             if(distance >= maxRange)
  73.             {
  74.                 UnGrapple();
  75.             }
  76.         }
  77.  
  78.         if (!isGrappled && !grapple.gameObject.activeSelf)
  79.         {
  80.             Charge();
  81.         }
  82.  
  83.         if (currentCharges == 0) manaOrb.Deactivate();
  84.         else manaOrb.Activate();
  85.     }
  86.  
  87.     void Charge()
  88.     {
  89.         if(currentCharges < maxCharges)
  90.         {
  91.             chargeTick += Time.deltaTime;
  92.             if (playerMovementController.isGrounded) chargeTick += Time.deltaTime * 3.0f;
  93.             if(chargeTick >= chargeTime)
  94.             {
  95.                 chargeTick -= chargeTime;
  96.                 currentCharges++;
  97.                 ExtensionMethods.PlayAudio(rechargeSound, 1.5f, Random.Range(0.8f, 1.2f));
  98.             }
  99.         }
  100.     }
  101.  
  102.     void FireGrapple()
  103.     {
  104.         // Deplete a Grapple Charge
  105.         currentCharges--; // Deplete graple charge
  106.  
  107.         isFired = true;
  108.         ExtensionMethods.PlayAudio(fireSound, 1.0f, Random.Range(0.8f, 1.2f));
  109.         grapple.transform.SetParent(null);
  110.         grapple.gameObject.SetActive(true);
  111.         grapple.rb.isKinematic = false;
  112.         grapple.collider.isTrigger = false;
  113.         grapple.transform.position = transform.position;
  114.         grapple.rb.position = transform.position;
  115.         grapple.rb.velocity = aimDirection * 100f;
  116.     }
  117.  
  118.     public void UnGrapple()
  119.     {
  120.         if (!isGrappled && !isFired) return;
  121.  
  122.         grapple.attachedTo = null;
  123.        
  124.         // If the grapple whiffed.
  125.         if(isFired && !isGrappled)
  126.         {
  127.             // Forgive the player with some grapple recharge
  128.             chargeTick += chargeTime * 0.75f;
  129.             if (chargeTick > chargeTime) chargeTick = chargeTime;
  130.         }
  131.  
  132.         ExtensionMethods.PlayAudio(retractSound, 1.0f, Random.Range(0.8f, 1.2f));
  133.         isGrappled = false;
  134.         isFired = false;
  135.         joint.enabled = false;
  136.         grapple.gameObject.SetActive(false);
  137.     }
  138.  
  139.     public void Grappled()
  140.     {
  141.         AudioSource audioSource = ExtensionMethods.PlayAudio(attachSound, 1.0f, Random.Range(0.8f, 1.2f), grapple.transform.position, ExtensionMethods.DEFAULT_AUDIO_RANGE);
  142.         audioSource.spatialBlend = 0.95f;
  143.         Debug.Log("Grappled");
  144.         isGrappled = true;
  145.         joint.enabled = true;
  146.         joint.distance = ((Vector2)(grapple.transform.position - transform.position)).magnitude;
  147.         if (joint.distance < 0.5f) joint.distance = 0.5f;
  148.  
  149.         // Adjust player velocity so they are not suddenly stopped on a grapple
  150.         // Did we grapple to a static object?
  151.         if (joint.connectedBody == grapple.rb)
  152.         {
  153.             Vector2 grappleDirection = grapple.transform.position - transform.position;
  154.             Vector2 velTowardsGrapple = Vector3.Project(playerBody.velocity, grappleDirection);
  155.             Debug.Log(grappleDirection);
  156.             Debug.Log(velTowardsGrapple);
  157.             playerBody.velocity -= velTowardsGrapple * 0.5f;
  158.             playerBody.velocity += playerBody.velocity.normalized * velTowardsGrapple.magnitude * 0.25f; // Re-add removed velocity in appropriate direction
  159.         }
  160.     }
  161.  
  162.     void AimLineUpdate()
  163.     {
  164.         aimLine.SetPosition(0, transform.position);
  165.         Vector3 position2 = 3f * aimDirection;
  166.         position2.z = transform.position.z;
  167.         aimLine.SetPosition(1, transform.position + position2);
  168.     }
  169.  
  170.     void RopeLineUpdate()
  171.     {
  172.         if (grapple.gameObject.activeSelf)
  173.         {
  174.             ropeLine.enabled = true;
  175.             ropeLine.SetPosition(0, transform.position);
  176.             ropeLine.SetPosition(1, grapple.transform.position);
  177.         }
  178.         else
  179.         {
  180.             ropeLine.enabled = false;
  181.         }
  182.     }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement