I have about 9 animations that I have to cycle between for walking and grabbing things with my character. What I want to know is if I can only use two separate animation clips that are empty and cycle through different ones. My original one that I had transitioning was the bottom photo with all the connections. It was to complicated and I was thinking if maybe there was a more simple way. So can I just call upon them in script and add them to the walking or reach? Here is my script so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed;
public Animation anim;
public Animator animator;
public int PlayerDirection;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
anim = GetComponent<Animation> ();
}
// Update is called once per frame
void Update () {
float axisX = Input.GetAxis ("Horizontal");
float axisY = Input.GetAxis ("Vertical");
if (Input.GetKeyDown (KeyCode.W)) {
//North
PlayerDirection = 1;
}
if (Input.GetKeyDown (KeyCode.S)) {
//South
PlayerDirection = 2;
}
if (Input.GetKeyDown (KeyCode.A)) {
//West
PlayerDirection = 3;
}
if (Input.GetKeyDown (KeyCode.D)) {
//East
PlayerDirection = 4;
}
switch (PlayerDirection) {
case 1:
Debug.Log ("here");
break;
}
transform.Translate (new Vector3 (axisX, axisY) * Time.deltaTime * speed);
}
}
I don’t think you can do that, and even if you could, it would make the animation transitions look crazy when you swapped out the clips mid-transition. The best way to do this would probably be to turn “Walking” and “Reach” into sub-statemachines, and inside each put a blend tree for the different directions. If that’s too complicated, the simplest way, which is most like just calling the clips directly from script, is to just have one state for each clip and have them all connected one-way from “Any State” to the clip, with each transition based on a different number… then you just set that number from your script and it will instantly switch from whatever state it’s in to whatever number you provide.
I agree with @makeshiftwings . You should really get some tutorial on how to create a good mechanim tree. There are way better ways you could do this like that sub-statemachines and blendtrees for your body parts, so you can walk left right and stil be able to override your upper body for example to turn somewhere. There are some tutorials out there, have a look at it. Also check out the standard assets and their mechanim animations.
It can be done in the animator, but you really have to know what your doing or else you can really mess up the animator.
to help explain, its best that we step back and examine how the animator works. you see the Animator Controller doesn’t remember which character is using it. its a project-level asset and its designed so that 50 different characters (I just picked random number, it can be any number) can all use the same Animator Controller. how this works is that the Animator Component holds state for the controller (this is why it has functions like SetBool, SetInt, etc.) and every frame the component talks to the Controller saying. “hey these are my current parameters, what state I’m in and how long I’ve been in this state, what should I do now?”.
The Controller takes that data and looks through the graph and replies “this is what you do this frame” and then immeadiately forgets about the Component that called it, because it needs to be ready to help some other character, and it doesn’t want to mess up and use the state of one character to tell what another character should do.
back to the question at hand, as I said you could do something nifty like create a StateMachineBehaviour that holds a list of Motions (base class for animation clips) and onStateEnter randomly picks one) but as I said if your not careful all characters would use the same random animation at the same time. it would be better for you had a Statemachine behaviour generate a random number between limits and then call Animator.SetInt(randomnumber) on exit. the Controller can then read that int when transitioning to know which animation to play.
**EDIT:**I’m a bit hesitant on this however, as its locking logic with animation, better to send a message to the character and the character can then pick a random number. this is better as it allows the character more freedom over which random animations it wants to use, or if it even wants to randomize in the first place. its just way more flexible, even if its a little bit more code.
As for simplifying your Animation Controller, be sure to look into using SubStates. and can really tidy up your graph and make it easier to maintain and follow. for instance all the animations that could be used by random, wrap them all in a sub state and now when you want to transition to a random animation the higher level simply links to the substate and leaving it to the internal state to figure out which specific animation to play