how to get my clawhand to grab my objectholder?

Viewed 61

enter image description here[![ hey guys so i need help please! i need my claw hand to be able to grab my object holder(orange thingy next to the red ball. my objectholder has a spring joint already so I want my clawhand to be able to grab it and pull it with the ball. When my hand opens then it shoots. But when I try to grab it, my hand goes right through. My claw hand has rigidbody,box collider,configuration joint and is animation by this code

public class open2close : MonoBehaviour
{
    public float speed;
    private Animation anim;
    Rigidbody rb;
   

    void Start()
    {
        anim = gameObject.GetComponent<Animation>();
        rb = GetComponent<Rigidbody>();
        
    }


    void Update()
    {
        //********************Open pincher ********************
        if (Input.GetKey(KeyCode.X))
        {
            anim.Play("clawopen");
        
            
        }
        //*******************Close pincher ********************
        if (Input.GetKey(KeyCode.Y))
        {
            anim.Play("clawclose");
           
        }


    }
}

as for my object holder it has box collider,spring joint, rigidbody, and rotation constraint. Can someone guide me or help me in what i can do thank you.

1 Answers

It is probably best to not have a collider on the claw hand. This is because you are mixing rigidbodies with animation, which can get messy. I would reccomend keeping everything, excpt for the box collider on the claw hand. Set it to a trigger. You can put booleans in the if statements to open and close the claw. This way, you can set the position of the objectholder to be at the claw (if they are touching).

Change your script to something like

public class open2close : MonoBehaviour
{
    public float speed;
    private Animation anim;
    [SerializeField] bool isClosed;
    Rigidbody rb;

    void Start()
    {
        anim = gameObject.GetComponent<Animation>();
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.X))
        {
            anim.Play("clawopen");
            isClosed = false;
        }
        if (Input.GetKey(KeyCode.Y))
        {
            anim.Play("clawclose");
            isClosed = true;
        }
    }
    void OnTriggerStay(Collider obj)
    {
        Rigidbody colRb = obj.attachedRigidbody;
        if (colRb != null && isClosed)
        {
            colRb.position = transform.position;
        }
    }
}

Let me know if this works and be specific! thanks :)

Related