Okay, first up, I’m very inexperienced in Unity, so please explain this in full detail and explain it like your talking to a noob, step by step please. (I don’t know any coding btw.) Okay, so we got that out of the way.
Next. I’m trying to apply an animation called ‘Run’ when you press Shift+W. I’m using my own model I created from MakeHuman, but I just don’t know how to apply the animation. A walk and crouch animation would be nice to explain too.
hi, all these topics are very nicely covered in these official tutorials:
Which should I start on?
Alright. The first thing you need is a running animation. Make sure it’s humanoid (click the model in the project view and hit rig, near the top). If it’s not, set it to humanoid. Make sure your model is humanoid, too. The reason for this is that animation retargeting only works between humanoid models (a generic or legacy animation will only work on the model it was made on).
Next: create an animator controller. Open it, and make an empty state (right click anywhere and hit new > empty state). It should be orange. Now, drag your animation into the animator.
Find the parameters tab and create a new boolean. Name it Run (case sensitive).
Now, create a transition from your empty state to your running state by right clicking the empty state, clicking “make transition,” and dragging the resulting arrow to your run state. Do the same from run to empty.
Click the transition between empty and run. In the inspector, find the “parameters” list and create a new one. Make sure it’s Run and that the dropdown list next to it is set to “true.” Now, find the “has exit time” box and uncheck it. If you don’t, the animator will wait until the current animation is done playing before transitioning to the next state.
Do the same for your other transition, but set Run to false.
Drag the animator controller into the animator component on your model and attach the following script to it. It should work.
using UnityEngine;
public class RunWithShiftW{
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if(Input.GetKey(KeyCode.Shift))
{
anim.SetBool("Run", Input.GetKey(KeyCode.W));
}
}
}
And now, scripting explanations! Input.GetKey() is used to tell whether or not a key is down. KeyCode is an enumerator that contains a code for every possible key. You can also use Input.GetButton() if you’ve set up inputs in the project settings.