Make the player go from a hook to another hook
Hi guys! I'm pretty new to Unity and coding in general, so i need your help!
So, currently, the cube is hooked on a hook, when the player drags the cube from the hook and next releases the mouse key, the cube will be launch away (Like AngryBirds in few words...). My idea was that if the cube collides with another hook while it is in the air, the cube will be hooked to this new hook and so on...
Here's the code "i made":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cube : MonoBehaviour
{
public Rigidbody2D rb;
public Rigidbody2D hook;
public float releaseTime = .15f;
public float maxDragDistance = 2f;
private bool isPressed = false;
private void Update()
{
if (isPressed)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Vector3.Distance(mousePos, hook.position) > maxDragDistance)
rb.position = hook.position + (mousePos - hook.position).normalized * maxDragDistance;
else
rb.position = mousePos;
}
}
void OnMouseDown()
{
isPressed = true;
rb.isKinematic = true;
}
void OnMouseUp()
{
isPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
}
IEnumerator Release()
{
yield return new WaitForSeconds(releaseTime);
GetComponent<SpringJoint2D>().enabled = false;
this.enabled = false;
Comment