using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grab : MonoBehaviour
{
[SerializeField] private float ThrowForce = 2f;
private void Update()
{
if(Input.GetKeyDown(KeyCode.K))
{
Destroy(GetComponent<FixedJoint2D>()); //Make sure to add fixedjointed2D and a rigidBody2D
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Weapon"))
{
Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
if (rb != null)
{
FixedJoint2D fj = transform.gameObject.AddComponent(typeof(FixedJoint2D)) as FixedJoint2D;
fj.connectedBody = rb;
}
else
{
FixedJoint2D fj = transform.gameObject.AddComponent(typeof(FixedJoint2D)) as FixedJoint2D;
}
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Weapon"))
{
Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
//Weapon carries momentum of the players
rb.velocity = gameObject.GetComponent<Rigidbody2D>().velocity;
//Addforce
rb.AddForce(transform.forward * ThrowForce, ForceMode2D.Impulse);
}
}
}