I’m trying to create a grappling hook that automatically finds grapple points. The problem is that instead of swinging, the character quickly shoots off to a random location and exits the game area. Can you help with this.I used this tutorial:https://youtu.be/dnNCVcVS6uw?si=LrEqIkOJOFKR66Hc
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GrapplingHook : MonoBehaviour
{
[Header("Scripts Ref:")]
public GrapplingRope grappleRope;
[Header("Layers Settings:")]
[SerializeField] private bool grappleToAll = false;
[SerializeField] private int grappableLayerNumber = 9;
[Header("Transform Ref:")]
public Transform gunHolder;
public Transform firePoint;
[Header("Physics Ref:")]
public SpringJoint2D m_springJoint2D;
public Rigidbody2D m_rigidbody;
[Header("Distance:")]
[SerializeField] private bool hasMaxDistance = false;
[SerializeField] private float maxDistnace = 20;
Collider2D[] hookPoints;
public GameObject nearestHookPoint;
private enum LaunchType
{
Transform_Launch,
Physics_Launch
}
[Header("Launching:")]
[SerializeField] private bool launchToPoint = true;
[SerializeField] private LaunchType launchType = LaunchType.Physics_Launch;
[SerializeField] private float launchSpeed = 1;
[Header("No Launch To Point")]
[SerializeField] private bool autoConfigureDistance = false;
[SerializeField] private float targetDistance = 3;
[SerializeField] private float targetFrequncy = 1;
[HideInInspector] public Vector2 grapplePoint;
[HideInInspector] public Vector2 grappleDistanceVector;
private void Start()
{
grappleRope.enabled = false;
m_springJoint2D.enabled = false;
}
private void Update()
{
float nearestDistance = float.MaxValue;
hookPoints = Physics2D.OverlapCircleAll(firePoint.position, maxDistnace, LayerMask.GetMask("GrapplePoint", "enemy"));
foreach (Collider2D hookPoint in hookPoints)
{
float dist = Vector2.Distance(hookPoint.transform.position, transform.position);
if (dist < nearestDistance)
{
nearestDistance = dist;
nearestHookPoint = hookPoint.gameObject;
}
}
if (nearestHookPoint != null && nearestHookPoint.CompareTag("swingPoint"))
{
launchToPoint = false;
}
else
{
launchToPoint = true;
}
}
public void Hook(InputAction.CallbackContext context)
{
if (context.started)
{
if (hookPoints != null && nearestHookPoint != null)
{
SetGrapplePoint();
Grapple(); // Grapple fonksiyonunu çağırmayı unutmayın
}
}
else if (context.performed)
{
if (hookPoints != null && nearestHookPoint != null)
{
Vector2 hookPointPosition = nearestHookPoint.transform.position;
if (launchToPoint && grappleRope.isGrappling)
{
if (launchType == LaunchType.Transform_Launch)
{
Vector2 firePointDistnace = firePoint.position - gunHolder.localPosition;
Vector2 targetPos = grapplePoint - firePointDistnace;
gunHolder.position = Vector2.Lerp(gunHolder.position, targetPos, Time.deltaTime * launchSpeed);
}
}
}
}
else if (context.canceled)
{
grappleRope.enabled = false;
m_springJoint2D.enabled = false;
m_rigidbody.gravityScale = 1;
}
}
void SetGrapplePoint()
{
if (nearestHookPoint == null) return;
Vector2 distanceVector = nearestHookPoint.transform.position - firePoint.position;
RaycastHit2D _hit = Physics2D.Raycast(firePoint.position, distanceVector.normalized);
if (_hit.collider != null) // _hit.collider ile hit kontrolü yapıyoruz
{
if (_hit.transform.gameObject.layer == grappableLayerNumber || grappleToAll)
{
if (Vector2.Distance(_hit.point, firePoint.position) <= maxDistnace || !hasMaxDistance)
{
grapplePoint = _hit.point;
grappleDistanceVector = grapplePoint - (Vector2)firePoint.position;
grappleRope.enabled = true;
}
}
}
}
public void Grapple()
{
m_springJoint2D.autoConfigureDistance = false;
if (!launchToPoint && !autoConfigureDistance)
{
m_springJoint2D.distance = targetDistance;
m_springJoint2D.frequency = targetFrequncy;
}
if (!launchToPoint)
{
if (autoConfigureDistance)
{
m_springJoint2D.autoConfigureDistance = true;
m_springJoint2D.frequency = 0;
}
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.enabled = true;
}
else
{
switch (launchType)
{
case LaunchType.Physics_Launch:
m_springJoint2D.connectedAnchor = grapplePoint;
Vector2 distanceVector = firePoint.position - gunHolder.position;
m_springJoint2D.distance = distanceVector.magnitude;
m_springJoint2D.frequency = launchSpeed;
m_springJoint2D.enabled = true;
break;
case LaunchType.Transform_Launch:
m_rigidbody.gravityScale = 0;
m_rigidbody.velocity = Vector2.zero;
break;
}
}
}
private void OnDrawGizmosSelected()
{
if (firePoint != null && hasMaxDistance)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(firePoint.position, maxDistnace);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrapplingRope : MonoBehaviour
{
[Header("General Refernces:")]
public GrapplingHook grapplingGun;
public LineRenderer m_lineRenderer;
[Header("General Settings:")]
[SerializeField] private int percision = 40;
[Range(0, 20)] [SerializeField] private float straightenLineSpeed = 5;
[Header("Rope Animation Settings:")]
public AnimationCurve ropeAnimationCurve;
[Range(0.01f, 4)] [SerializeField] private float StartWaveSize = 2;
float waveSize = 0;
[Header("Rope Progression:")]
public AnimationCurve ropeProgressionCurve;
[SerializeField] [Range(1, 50)] private float ropeProgressionSpeed = 1;
float moveTime = 0;
[HideInInspector] public bool isGrappling = true;
bool strightLine = true;
private void OnEnable()
{
moveTime = 0;
m_lineRenderer.positionCount = percision;
waveSize = StartWaveSize;
strightLine = false;
LinePointsToFirePoint();
m_lineRenderer.enabled = true;
}
private void OnDisable()
{
m_lineRenderer.enabled = false;
isGrappling = false;
}
private void LinePointsToFirePoint()
{
for (int i = 0; i < percision; i++)
{
m_lineRenderer.SetPosition(i, grapplingGun.firePoint.position);
}
}
private void Update()
{
moveTime += Time.deltaTime;
DrawRope();
}
void DrawRope()
{
if (!strightLine)
{
if (m_lineRenderer.GetPosition(percision - 1).x == grapplingGun.grapplePoint.x)
{
strightLine = true;
}
else
{
DrawRopeWaves();
}
}
else
{
if (!isGrappling)
{
grapplingGun.Grapple();
isGrappling = true;
}
if (waveSize > 0)
{
waveSize -= Time.deltaTime * straightenLineSpeed;
DrawRopeWaves();
}
else
{
waveSize = 0;
if (m_lineRenderer.positionCount != 2) { m_lineRenderer.positionCount = 2; }
DrawRopeNoWaves();
}
}
}
void DrawRopeWaves()
{
for (int i = 0; i < percision; i++)
{
float delta = (float)i / ((float)percision - 1f);
Vector2 offset = Vector2.Perpendicular(grapplingGun.grappleDistanceVector).normalized * ropeAnimationCurve.Evaluate(delta) * waveSize;
Vector2 targetPosition = Vector2.Lerp(grapplingGun.firePoint.position, grapplingGun.grapplePoint, delta) + offset;
Vector2 currentPosition = Vector2.Lerp(grapplingGun.firePoint.position, targetPosition, ropeProgressionCurve.Evaluate(moveTime) * ropeProgressionSpeed);
m_lineRenderer.SetPosition(i, currentPosition);
}
}
void DrawRopeNoWaves()
{
m_lineRenderer.SetPosition(0, grapplingGun.firePoint.position);
m_lineRenderer.SetPosition(1, grapplingGun.grapplePoint);
}
}

