From 92be2fc57f7d73a883de348246cd288dcdef2cc3 Mon Sep 17 00:00:00 2001 From: Soulaha Balde Date: Wed, 19 Oct 2022 15:35:46 -0400 Subject: [PATCH] Rope lerps to grapple point --- Assets/GrappleHook.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Assets/GrappleHook.cs b/Assets/GrappleHook.cs index d448ac8..a0ee4d5 100644 --- a/Assets/GrappleHook.cs +++ b/Assets/GrappleHook.cs @@ -8,7 +8,9 @@ public class GrappleHook : MonoBehaviour [SerializeField] private float maxGrappleDist = 200; [SerializeField] - private LayerMask grappleableLayer; // TODO create layermask + private float grappleExtendSpeed = 20; + [SerializeField] + private LayerMask grappleableLayer; [SerializeField] private Transform gunPos; private Vector3 hitPosLocal; @@ -16,6 +18,7 @@ public class GrappleHook : MonoBehaviour private LineRenderer lr; RaycastHit hit; private bool grappled = false; + private Vector3 currGrappleEndPos; [SerializeField] private Transform hitMarker; //TODO obj this should be a sprite or something idk // Start is called before the first frame update @@ -35,7 +38,7 @@ public class GrappleHook : MonoBehaviour EndGrapple(); } if(grappled){ - DrawRope(); + DrawRope(Time.deltaTime); if(Input.GetMouseButtonDown(1)){ joint.minDistance = 0f; joint.maxDistance = 0f; @@ -61,16 +64,19 @@ public class GrappleHook : MonoBehaviour } } - private void DrawRope(){ + private void DrawRope(float deltaTime){ + // TODO animate a wiggle on the rope if(!grappled)return; - // TODO Draw gradually towards point lr.SetPosition(0, gunPos.position); + Vector3 endPoint; if(hit.rigidbody != null){ - lr.SetPosition(1, hit.transform.TransformPoint(hitPosLocal)); + endPoint = hit.transform.TransformPoint(hitPosLocal); + }else{ - lr.SetPosition(1, hit.point); + endPoint = hit.point; } - + currGrappleEndPos = Vector3.Lerp(currGrappleEndPos, endPoint, deltaTime * grappleExtendSpeed); + lr.SetPosition(1, currGrappleEndPos); if(!lr.enabled)lr.enabled = true; } @@ -97,11 +103,14 @@ public class GrappleHook : MonoBehaviour joint.spring = 4.5f * 5f; joint.damper = 7f; joint.massScale = 4.5f; + + currGrappleEndPos = transform.position; } private void EndGrapple(){ grappled = false; lr.enabled = false; + Destroy(joint); }