Greetings to all of you
i am trying to make my 3d person character grab an object with joints and be able to throw it with force!
I wrote this script but nothing works! I am trying to make the script able to take the rigidbody it collided with and use it (i hope i made clear what i have in my head couse i am not that good with english)
public class grab : MonoBehaviour
{
// Start is called before the first frame update
public int BreakF = 500;
public int BreakT = 500;
private bool Holding = false;
public Rigidbody rb;
public float thrust;
void Start()
{
Holding = false;
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Interact" && Input.GetKey(KeyCode.G))
{ // creates joint
FixedJoint joint = gameObject.AddComponent<FixedJoint>();
// sets joint position to point of contact
joint.anchor = col.contacts[0].point;
// conects the joint to the other object
joint.connectedBody = col.contacts[0].otherCollider.transform.GetComponentInParent<Rigidbody>();
// Stops objects from continuing to collide and creating more joints
joint.enableCollision = false;
joint.breakForce = BreakF;
joint.breakTorque = BreakT;
Holding = true;
rb = GetComponent<Rigidbody>();
}
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.G) && (Holding = true))
{ rb.AddForce(transform.forward * thrust); }
}
}