Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GrapplingHook : MonoBehaviour
- {
- [Header("References")]
- public LayerMask whatIsGrappleAble;
- public MovementController playerMovementController;
- public Rigidbody2D playerBody;
- public DistanceJoint2D joint;
- public LineRenderer aimLine;
- public LineRenderer ropeLine;
- public Grapple grapple;
- [Header("Parameters")]
- public float maxRange = 25f;
- [Header("Charges")]
- public int maxCharges;
- int currentCharges;
- public float chargeTime;
- float chargeTick;
- public ManaOrb manaOrb;
- [Header("Info")]
- public bool isGrappled = false;
- public bool isFired = false;
- Vector2 aimDirection;
- [Header("SFX")]
- public AudioClip fireSound;
- public AudioClip attachSound;
- public AudioClip retractSound;
- public AudioClip rechargeSound;
- public AudioClip falseFireSound;
- // Start is called before the first frame update
- void Start()
- {
- grapple.transform.SetParent(null);
- aimLine.positionCount = 2;
- ropeLine.positionCount = 2;
- joint.enabled = false;
- currentCharges = maxCharges;
- }
- // Update is called once per frame
- void Update()
- {
- aimDirection = ((Vector2)(Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position)).normalized;
- AimLineUpdate();
- RopeLineUpdate();
- if(Input.GetButtonDown("Fire1"))
- {
- if (currentCharges > 0) FireGrapple();
- else ExtensionMethods.PlayAudio(falseFireSound, 1.0f, Random.Range(0.8f, 1.2f));
- }
- if(Input.GetButtonUp("Fire1"))
- {
- UnGrapple();
- }
- // Check if grapple out of range
- if(!isGrappled && grapple.gameObject.activeSelf)
- {
- float distance = Vector2.Distance(transform.position, grapple.transform.position);
- if(distance >= maxRange)
- {
- UnGrapple();
- }
- }
- if (!isGrappled && !grapple.gameObject.activeSelf)
- {
- Charge();
- }
- if (currentCharges == 0) manaOrb.Deactivate();
- else manaOrb.Activate();
- }
- void Charge()
- {
- if(currentCharges < maxCharges)
- {
- chargeTick += Time.deltaTime;
- if (playerMovementController.isGrounded) chargeTick += Time.deltaTime * 3.0f;
- if(chargeTick >= chargeTime)
- {
- chargeTick -= chargeTime;
- currentCharges++;
- ExtensionMethods.PlayAudio(rechargeSound, 1.5f, Random.Range(0.8f, 1.2f));
- }
- }
- }
- void FireGrapple()
- {
- // Deplete a Grapple Charge
- currentCharges--; // Deplete graple charge
- isFired = true;
- ExtensionMethods.PlayAudio(fireSound, 1.0f, Random.Range(0.8f, 1.2f));
- grapple.transform.SetParent(null);
- grapple.gameObject.SetActive(true);
- grapple.rb.isKinematic = false;
- grapple.collider.isTrigger = false;
- grapple.transform.position = transform.position;
- grapple.rb.position = transform.position;
- grapple.rb.velocity = aimDirection * 100f;
- }
- public void UnGrapple()
- {
- if (!isGrappled && !isFired) return;
- grapple.attachedTo = null;
- // If the grapple whiffed.
- if(isFired && !isGrappled)
- {
- // Forgive the player with some grapple recharge
- chargeTick += chargeTime * 0.75f;
- if (chargeTick > chargeTime) chargeTick = chargeTime;
- }
- ExtensionMethods.PlayAudio(retractSound, 1.0f, Random.Range(0.8f, 1.2f));
- isGrappled = false;
- isFired = false;
- joint.enabled = false;
- grapple.gameObject.SetActive(false);
- }
- public void Grappled()
- {
- AudioSource audioSource = ExtensionMethods.PlayAudio(attachSound, 1.0f, Random.Range(0.8f, 1.2f), grapple.transform.position, ExtensionMethods.DEFAULT_AUDIO_RANGE);
- audioSource.spatialBlend = 0.95f;
- Debug.Log("Grappled");
- isGrappled = true;
- joint.enabled = true;
- joint.distance = ((Vector2)(grapple.transform.position - transform.position)).magnitude;
- if (joint.distance < 0.5f) joint.distance = 0.5f;
- // Adjust player velocity so they are not suddenly stopped on a grapple
- // Did we grapple to a static object?
- if (joint.connectedBody == grapple.rb)
- {
- Vector2 grappleDirection = grapple.transform.position - transform.position;
- Vector2 velTowardsGrapple = Vector3.Project(playerBody.velocity, grappleDirection);
- Debug.Log(grappleDirection);
- Debug.Log(velTowardsGrapple);
- playerBody.velocity -= velTowardsGrapple * 0.5f;
- playerBody.velocity += playerBody.velocity.normalized * velTowardsGrapple.magnitude * 0.25f; // Re-add removed velocity in appropriate direction
- }
- }
- void AimLineUpdate()
- {
- aimLine.SetPosition(0, transform.position);
- Vector3 position2 = 3f * aimDirection;
- position2.z = transform.position.z;
- aimLine.SetPosition(1, transform.position + position2);
- }
- void RopeLineUpdate()
- {
- if (grapple.gameObject.activeSelf)
- {
- ropeLine.enabled = true;
- ropeLine.SetPosition(0, transform.position);
- ropeLine.SetPosition(1, grapple.transform.position);
- }
- else
- {
- ropeLine.enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement