Hello everyone,
Can someone could help me to make a hook that fulfills the functions of grabbing and attracting objects and also when is hooked to a wall drag the player to the wall?
This is the code and for now only fulfills the function of dragging the player to the walls, I’m using the hook of zelda a link to the past to reference:
public class GrapplingGun : MonoBehaviour
{
[Header("Scripts:")]
public GrappleRope grappleRope;
[Header("Layer Settings:")]
[SerializeField] private bool grappleToAll = false;
[SerializeField] private int grappableLayerNumber = 9;
//[SerializeField] private int arrastableLayerNumber = 10; //
//ATRACCION VAR
[SerializeField] private float attractionForce;
[SerializeField] private float attractionRadius = 3f;
[Header("Transform Refrences:")]
public Transform gunHolder;
public Transform gunPivot;
public Transform firePoint;
[Header("Distance:")]
[SerializeField] private bool hasMaxDistance = true;
[SerializeField] private float maxDistance = 4;
[Header("Launching")]
[SerializeField] private bool launchToPoint = true;
[SerializeField] private LaunchType Launch_Type = LaunchType.Transform_Launch;
[Range(0, 5)] [SerializeField] private float launchSpeed = 5;
[Header("No Launch To Point")]
[SerializeField] private bool autoCongifureDistance = false;
[SerializeField] private float targetDistance = 3;
[SerializeField] private float targetFrequency = 3;
private enum LaunchType
{
Transform_Launch,
Physics_Launch,
}
[Header("Component Refrences:")]
public SpringJoint2D m_springJoint2D;
[HideInInspector] public Vector2 grapplePoint;
[HideInInspector] public Vector2 DistanceVector;
public Rigidbody2D ballRigidbody;
private bool atraer = false; //
private void Start()
{
grappleRope.enabled = false;
m_springJoint2D.enabled = false;
ballRigidbody.gravityScale = 1;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
SetGrapplePoint();
}
else if (Input.GetKey(KeyCode.R)) //Manejo de rotacion y lanzamiento
{
if (grappleRope.enabled)
{
}
if (launchToPoint && grappleRope.isGrappling)
{
// Usa Vector3.Lerp para mover suavemente gunHolder hacia grapplePoint
// con una velocidad controlada por launchSpeed
if (Launch_Type == LaunchType.Transform_Launch)
{
gunHolder.position = Vector3.Lerp(gunHolder.position, grapplePoint, Time.deltaTime * launchSpeed);
}
}
}
else if (Input.GetKeyUp(KeyCode.Q))
{
//DEHABILITA EL GANCHO
grappleRope.enabled = false;
m_springJoint2D.enabled = false;
ballRigidbody.gravityScale = 1;
}
}
void SetGrapplePoint() //ESTABLECE EL PUNTO DE AGARRE
{
//LANZAR UN RAYO DESDE EL PUNTO DE LANZAMIENTO
if (Physics2D.Raycast(firePoint.position, transform.right))
{
RaycastHit2D _hit = Physics2D.Raycast(firePoint.position, transform.right);
if ((_hit.transform.gameObject.layer == grappableLayerNumber || grappleToAll) && ((Vector2.Distance(_hit.point, firePoint.position) <= maxDistance) || !hasMaxDistance))
{
grapplePoint = _hit.point;
DistanceVector = grapplePoint - (Vector2)gunPivot.position;
grappleRope.enabled = true;
}
}
}
//FUNCION QUE MANEJA LA LOGICA DE LANZAMIENTO Y ATRACCION
public void Grapple()
{
//LOGICA DE LANZAMIENTO Y ATRACCION
if (!launchToPoint && !autoCongifureDistance)
{
// Establece la distancia del resorte y la frecuencia según los valores de targetDistance y targetFrequency
m_springJoint2D.distance = targetDistance;
m_springJoint2D.frequency = targetFrequency;
}
if (!launchToPoint)
{
if (autoCongifureDistance)
{
m_springJoint2D.autoConfigureDistance = true;
m_springJoint2D.frequency = 0;
}
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.enabled = true;
//ATRACCION DE OBJETOS CERCANOS
Collider2D[] nearbyColliders = Physics2D.OverlapCircleAll(grapplePoint, attractionRadius); // Detectar todos los colliders cercanos dentro del radio de atracción desde el punto de agarre
foreach (Collider2D collider in nearbyColliders) // Iterar a través de cada collider en la lista de colliders cercanos
{
Rigidbody2D rb = collider.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 attracDirection = ((Vector2)gunPivot.position - rb.position).normalized; // Calcular la dirección de atracción desde el pivote de la pistola hacia el objeto
rb.AddForce(attracDirection * attractionForce, ForceMode2D.Force); // Aplicar una fuerza de atracción al objeto utilizando la dirección calculada y la fuerza de atracción configurada
}
}
//AQUI DEBERIA DE SER LA ATRACCION HACIA EL JUGADOR
Vector2 attractionDirection = ((Vector2)gunPivot.position - grapplePoint).normalized;
ballRigidbody.AddForce(attractionDirection * attractionForce, ForceMode2D.Force);
}
else
{
if (Launch_Type == LaunchType.Transform_Launch)
{
ballRigidbody.gravityScale = 0;
ballRigidbody.velocity = Vector2.zero;
}
if (Launch_Type == LaunchType.Physics_Launch)
{
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.distance = 0;
m_springJoint2D.frequency = launchSpeed;
m_springJoint2D.enabled = true;
}
}
}
private void OnDrawGizmos()
{
if (hasMaxDistance)
{
//DIBUJA UNA ESFERA SI TIENE DISTANCIA MAX CONFIGURADA
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(firePoint.position, maxDistance);
}
}
}