- Home /
Trigger in child - boxcollider2d without trigger on parent issues.
hi there!
So, i'm trying to have my character grab an object, set the transform.position of the object to the hand of the character and then be able to throw it. The issue here has been adressed before, i'm sure:
-the object to grab has to have a Dynamic Rigidbody2d (so I can throw it afterwards);
-the character has to be able to "pass through" the object.
If the object has a dynamic rigidbody, the character and the object bump onto each other.
If I set it to trigger, the object falls into eternity. If I put both objects in different layers (having previously set the layer collision matrix properly), the character can indeed pass through the object, but then it can't be grabbed.
To fix this, I kept them on separate layers, I set the box collider of the object to grab to non-trigger, and added a child object with only a trigger boxcollider2d component and on a different layer. Then, on the character, I have this script:
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("PickUp"))
{
if (Input.GetKeyDown(KeyCode.E) && handsFull == false)
{
animator.Play("Crapolo_Grab");
collision.gameObject.transform.parent = carryingHand.transform;
collision.gameObject.transform.position = carryingHand.transform.position;
handsFull = true;
throwable = collision.gameObject;
}
}
}
What it does now is grab ONLY the trigger child of the object to grab :-P I tried to fix it by adding something like this:
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("PickUp"))
{
if (Input.GetKeyDown(KeyCode.E) && handsFull == false)
{
animator.Play("Crapolo_Grab");
Transform collisionParent = collision.gameObject.GetComponentInParent<Transform>();
collisionParent.parent = carryingHand.transform;
collisionParent.position = carryingHand.transform.position;
handsFull = true;
throwable = collision.gameObject;
}
}
}
but meh. Keeps grabbing the trigger child only.
Can you guys help me have the character grab the whole object, and not only the trigger child? Or help me think this in a different way :-P
Thanks guys!
Judit
Your answer
Follow this Question
Related Questions
trigger problem 0 Answers
Trigger 2D (another gameObject) 4 Answers
Press a key to damage all enemies tagged as Enemy separately 4 Answers
Need help with Raycast2D 0 Answers