0
\$\begingroup\$

I'm trying to put an object (for example a sword) in my player's hand. To do so i set the player's hand transform as the object's parent, in order to have the same movement for the object and the hand. No problem here.

The real issue starts as soon as i set the sword position to the hand position. My player starts to move backward.

One of my doubt was that my sword has a rigid body and my player too, so it could have an impact on it because I know that you can set a rigid body as a rigid body child.

So I was wondering if it's the right way to do it.

Edit

Here is my script to pickup an item:

 public void PickupItem()
    {
        Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit interactionHit;

        if (Physics.Raycast(interactionRay, out interactionHit, InteractionMaxDistance))
        {
            if (interactionHit.collider.tag == "LootableItem")
            {
                Item item = interactionHit.collider.gameObject.GetComponent<Item>();
                GetComponent<CharacterStats>().AddStats(item.Stats);

                _hand = TransformUtil.FindChild("mixamorig:RightHandMiddle1", _player);
                item.GetComponent<Rigidbody>().isKinematic = true;
                item.GetComponent<Rigidbody>().useGravity = false;

                item.transform.parent = _hand;
                item.transform.position = _hand.position;
                item.transform.rotation = _hand.rotation;
            }
        }
    }

My script for player movement :

void Update()
    {
        float translation = (Input.GetAxis("Vertical") * Speed) * Time.deltaTime;
        float rotation = (Input.GetAxis("Horizontal") * RotationSpeed) * Time.deltaTime;
        transform.Translate(0, 0, translation);
        transform.Rotate(0, rotation, 0);

        if (Input.GetButtonDown("Jump"))
        {
            _animator.SetTrigger("triggerJump");
            return;
        }


        if (Math.Abs(translation) < 0.01 && Math.Abs(rotation) < 0.01)
        {
            Speed = 1;
        }
        else
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                Speed = 4;
                RotationSpeed = 150;
            }
            else
            {
                Speed = 2;
                RotationSpeed = 200;
            }
        }

        _animator.SetFloat("speed", Speed);
        _animator.SetFloat("rotation", rotation);
        _animator.SetFloat("translation", translation);
    }

I took my character from Mixamo, same for the animaitons.

\$\endgroup\$
2
  • \$\begingroup\$ First of all i don't understand why you need to add a rigidbody to the sword, you could just set the object as a child of the player's hand. But if actually need a rigdbody on the sword, you can just set it to isKinematic = true when the sword is set as a child and isKinematic = false when the sword is detached from the player's hand. The fact that your player starts going backwards is really weird, and not understandable from the amount of details you provided us. In order to diagnose the problem we should now things like how you are moving the player, how you have you player set up etc.. \$\endgroup\$
    – user100681
    Commented Dec 3, 2017 at 10:43
  • \$\begingroup\$ I need a rigidbody on my sword becéuse I want it to be affected by physics, for example when I drop the sword I want it to actually drop on the ground. I already use isKinematic. I'm going to add more details in the main post, thanks. \$\endgroup\$ Commented Dec 3, 2017 at 13:52

2 Answers 2

0
\$\begingroup\$

It sounds like the physics engine might be finding a collision between the player's dynamic collider and the sword's, and is trying to move them apart to resolve the collision.

If you want the player to ignore self-collisions with objects they've picked up, you can call:

Physics.IgnoreCollision(playerCollider, itemCollider, true);

Once you've dropped the item, you can re-enable collisions between it and the player using:

Physics.IgnoreCollision(playerCollider, itemCollider, false);
\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your answer but unfortunatly it's not workin \$\endgroup\$ Commented Dec 3, 2017 at 15:03
0
\$\begingroup\$

I found a solution, it's not the best but it works.

When I equip my sword I destroy the Rigidbody and set it's Collider to isTrigger in order to detect collisions.

Destroy(item.getComponent<RigidBody>);
item.getComponent<Collider>().isTrigger = true;

And once I drop it, i add a new rigid Rigidbody and I switch isTrigger to false.

item.addComponent<RigidBody>();
item.getComponent<Collider>().isTrigger = false;

ps: If anyone has a better solution I would really appreciate.

\$\endgroup\$
5
  • \$\begingroup\$ You could probably just disable/reenable the rigidbody component rather than destroying it. \$\endgroup\$ Commented Dec 12, 2017 at 16:53
  • \$\begingroup\$ I tried but haven't found anything, all the solutions on the forums are deprecated and have benn removed from unity. \$\endgroup\$ Commented Dec 12, 2017 at 17:55
  • \$\begingroup\$ Do you havea solution to do it ? \$\endgroup\$ Commented Dec 12, 2017 at 17:55
  • \$\begingroup\$ Set isKinematic to true. \$\endgroup\$ Commented Dec 12, 2017 at 19:13
  • \$\begingroup\$ I already did that, look at the code in the main post. And isKinematic doesn't disable the rigidBody, it just stop forces, collisions or joints to affect the rigidBody \$\endgroup\$ Commented Dec 12, 2017 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .