So basically I want when in range of the boss enemy it throws a dagger that when it hits the player the player movement is stopped for the duration of the hook and pulls the player towards him then the hook detaches after a set time and the player movement is back tried doing it using raycasthit2d but it doesnt react except if im on the right side of the boss and when so it pulls me and the boss and throws us both randomly and aggressively here is the code i used in the script and the script’s place is on the boss
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrapplingHook : MonoBehaviour
{
public LineRenderer lr;
public LayerMask grappleMask;
public float moveSpeed = 2;
public float grappleLength = 5;
[Min(1)]
public int maxPoints = 3;
public float GrappleSpeed;
public float lineOfSight;
private Rigidbody2D player;
public float timer;
public float maxAttach;
private Rigidbody2D rig;
private List<Vector2> points = new List<Vector2>();
private void Start()
{
lr.positionCount = 0;
player = GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>();
timer = 0;
}
// Update is called once per frame
void Update()
{
float distanceFromPlayer = Vector2.Distance(player.position, transform.position);
Vector2 targetPos = player.position;
Vector2 direction = Vector2.Lerp(transform.position, targetPos, GrappleSpeed * Time.deltaTime);
if (distanceFromPlayer <= lineOfSight)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, grappleLength, grappleMask);
if (hit.collider != null)
{
Vector2 hitPoint = hit.point;
points.Add(hitPoint);
if (points.Count > maxPoints)
{
points.RemoveAt(0);
}
}
}
if (points.Count > 0)
{
player.isKinematic = true;
Vector2 moveFrom = centriod(points.ToArray());
player.MovePosition(Vector2.MoveTowards(moveFrom, (Vector2)transform.position, Time.deltaTime * moveSpeed));
lr.positionCount = 0;
lr.positionCount = points.Count * 2;
for (int n = 0, j = 0; n < points.Count * 2; n += 2, j++)
{
lr.SetPosition(n, transform.position);
lr.SetPosition(n + 1, points[j]);
}
}
if (timer >= maxAttach)
{
Detatch();
}
}
Vector2 centriod(Vector2[] points)
{
Vector2 center = Vector2.zero;
foreach (Vector2 point in points)
{
center += point;
}
center /= points.Length;
return center;
}
public void Detatch()
{
timer = 0;
lr.positionCount = 0;
points.Clear();
player.isKinematic = false;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, lineOfSight);
Gizmos.DrawWireSphere(transform.position, grappleLength);
}
}