Rope lerps to grapple point

This commit is contained in:
Soulaha Balde 2022-10-19 15:35:46 -04:00
parent 62267b6f2e
commit 92be2fc57f

View File

@ -8,7 +8,9 @@ public class GrappleHook : MonoBehaviour
[SerializeField] [SerializeField]
private float maxGrappleDist = 200; private float maxGrappleDist = 200;
[SerializeField] [SerializeField]
private LayerMask grappleableLayer; // TODO create layermask private float grappleExtendSpeed = 20;
[SerializeField]
private LayerMask grappleableLayer;
[SerializeField] [SerializeField]
private Transform gunPos; private Transform gunPos;
private Vector3 hitPosLocal; private Vector3 hitPosLocal;
@ -16,6 +18,7 @@ public class GrappleHook : MonoBehaviour
private LineRenderer lr; private LineRenderer lr;
RaycastHit hit; RaycastHit hit;
private bool grappled = false; private bool grappled = false;
private Vector3 currGrappleEndPos;
[SerializeField] [SerializeField]
private Transform hitMarker; //TODO obj this should be a sprite or something idk private Transform hitMarker; //TODO obj this should be a sprite or something idk
// Start is called before the first frame update // Start is called before the first frame update
@ -35,7 +38,7 @@ public class GrappleHook : MonoBehaviour
EndGrapple(); EndGrapple();
} }
if(grappled){ if(grappled){
DrawRope(); DrawRope(Time.deltaTime);
if(Input.GetMouseButtonDown(1)){ if(Input.GetMouseButtonDown(1)){
joint.minDistance = 0f; joint.minDistance = 0f;
joint.maxDistance = 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; if(!grappled)return;
// TODO Draw gradually towards point
lr.SetPosition(0, gunPos.position); lr.SetPosition(0, gunPos.position);
Vector3 endPoint;
if(hit.rigidbody != null){ if(hit.rigidbody != null){
lr.SetPosition(1, hit.transform.TransformPoint(hitPosLocal)); endPoint = hit.transform.TransformPoint(hitPosLocal);
}else{ }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; if(!lr.enabled)lr.enabled = true;
} }
@ -97,11 +103,14 @@ public class GrappleHook : MonoBehaviour
joint.spring = 4.5f * 5f; joint.spring = 4.5f * 5f;
joint.damper = 7f; joint.damper = 7f;
joint.massScale = 4.5f; joint.massScale = 4.5f;
currGrappleEndPos = transform.position;
} }
private void EndGrapple(){ private void EndGrapple(){
grappled = false; grappled = false;
lr.enabled = false; lr.enabled = false;
Destroy(joint); Destroy(joint);
} }