- Home /
Creating a function to use as an event in an animation?
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
Your question consists out of two parts. How to pick up a sword, and how to trigger the picking up during animation. Do you already know how to do one of these?
its the triggering the picking up i cant do, binding the sword two his hand in mid animation
Answer by Chronos-L · Mar 28, 2013 at 12:58 AM
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 ) {
...
}
okay well, when i do this, there are a couple of issues that pop up when i do this, firstly when i parent the sword to the sword parent (the sword parent being his hand) the sword will follow his hand all through the animation when i just want it to follow his hand after hes grabbed the sword, is there a way to parent the sword to his hand in mid animation? also when i put the code in, it tells me "the type of namespace name "$$anonymous$$onoBehaviour" cannot be found" what does this mean? how can i fix this?
Look at animation event, you can call the parenting-function in mid animation.
Start your .cs script like so:
using UnityEngine;
public class PickUp : $$anonymous$$onoBehaviour {
...
}
The namespace $$anonymous$$onoBehaviour is defined in the UnityEngine. You should import it to be able to use it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Animation Event don't show C# methods 1 Answer
Need help with some OnTrigger Scripting 2 Answers
Good design when starting animation and triggering event in animation? 2 Answers
Animation stoping!! 1 Answer