- Home /
Ragdoll only physics grab object
Hey guys so i am making an active ragdoll game and every script is physics based (i apply constant forces to my character parts to move them) ...so i have this grab script
public class grab : MonoBehaviour
{
public GameObject item;
public GameObject tempParent;
private bool cangrab;
public Transform guide;
// Start is called before the first frame update
void Start()
{
cangrab = false;
}
// Update is called once per frame
void Update()
{
{
if (Input.GetButton("grab"))
GroundCheck();
GetComponent<ConstantForce>().force = new Vector3(80, 10, 100);
{
if (cangrab == true)
pickup();
}
GetComponent<ConstantForce>().force = new Vector3(0, 0, 0) * Time.deltaTime;
drop();
}
}
void GroundCheck()
{
RaycastHit hit;
float distance = 1f;
Vector3 dir = new Vector3(1, 1, 1);
if (Physics.Raycast(transform.position, dir, out hit, distance) && hit.transform.tag == "interactable")
{
cangrab = true;
}
else
{
cangrab = false;
}
}
void pickup()
{
item.GetComponent<Rigidbody>().useGravity = false;
item.GetComponent<Rigidbody>().isKinematic = true;
item.transform.position = guide.transform.position;
item.transform.rotation = guide.transform.rotation;
item.transform.parent = tempParent.transform;
}
void drop()
{
item.GetComponent<Rigidbody>().useGravity = true;
item.GetComponent<Rigidbody>().isKinematic = false;
item.transform.parent = null;
item.transform.position = guide.transform.position;
}
}
But it doesnt work at all....the hand isnt moving when i press grab button (so no frces applied to it) and the item stays in place and when i am changing the transform guide to the characters hand ...when i hit play it instantly teleports to my player and rotating around like crazy! can you suggest another script without raycast(with collision maybe) or what can i do to fix this?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612202256im_/https://answers.unity.com/themes/thub/images/avi.jpg)