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- ();
GetComponent().AddStats(item.Stats);
_hand = TransformUtil.FindChild("mixamorig:RightHandMiddle1", _player);
item.GetComponent().isKinematic = true;
item.GetComponent().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.
Answer
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);
item.getComponent().isTrigger = true;
And once I drop it, i add a new rigid Rigidbody
and I switch isTrigger
to false.
item.addComponent();
item.getComponent().isTrigger = false;
ps: If anyone has a better solution I would really appreciate.
No comments:
Post a Comment