Hello, i am relatively new to animating in unity and i am trying to create an animation where my characters picks up a sword and i am trying to create an event so when he grabs the sword, it binds to his hand so when i move his hand, the sword will move with it. how exactly would i go about doing this? sorry, i’m still learning
You need to have these things:
- A trigger that will detect the sword on the ground/closet
- A bone/empty object parented to the palm, this will serve as the parent of the sword, you need to orientate this bone/object so that when the sword is held, the sword has the correct orientation
Steps
- Detect for sword
- If pick-up key is pressed, and sword is detected, Go to step 3; else go back to Step 1
- Play pick-up animation; Time step 4 and 5 correctly using animation event
- Copy the orientation of the sword-parent to the sword itself
- Parent the sword to the sword-parent
Example script
public class PickUp : MonoBehaviour {
public Transform swordParent;
private Transform sword;
void Update() {
if( Input.GetKeyDown( KeyCode.C ) && sword != null ) {
animation.Play("Pickup");
}
}
void OnTriggerEnter( Collider other ) {
if( other.gameObject.CompareTag("Sword") ) {
sword = other.gameObject.Transform ;
}
}
void OnTriggerExit( Collider other ) {
if( other.gameObject.Transform == sword ) {
sword = null;
}
}
void PickUpSword() {
sword.parent = swordParent;
sword.localLocation = Vector3.zero;
sword.localRotation = Quaternion.identity;
sword.localScale = Vector3.one;
}
}
You will insert an animation-event in the Pickup animation clip to call the PickUpSword()
. Please note that this is an example, you will have to do the integration yourself as I do not know how you structure/write your script.
You can even split the detecting function to another script, for example: SwordDetectArea.cs; so in your animation-controller script, you will do something like this:
if( Input.GetKeyDown( KeyCode.C ) && swordDetectArea.sword != null ) {
...
}