- Home /
The animation state could not be played because it couldn't be found!
Hi, trying to make a custom FPS player with self-made animations but when I use this code attached to the First Person Controller and press shift, it gives me the error in the question.
Here's the code I'm using, it references to animations in the Main Camera.
childObject= GameObject.Find("Main Camera");
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
speed = runSpeed;
childObject.animation.Play("fpsrun");
}
else {
childObject.animation.Play("fpswalk");
}
Anyone know a quick fix for this? The animations are in the Assets folder. Do they need to be in a special folder somewhere? Also, they are attached to an animation tab in the inspector in Main Camera.
Any help? Thanks!
What is ch??? Please post full script so I can troubleshoot.
Is the game object which contains this script animated ? or is the childObject animated ?
So your animations only consist on moving the camera ?
Do you have exactly one object called "$$anonymous$$ain Camera", and does it contains the animations "fpsrun" and "fpswalk" (I think it is case sensitive, so check it carefully in the inspector) ?
Why are you putting local variables and defining childObject in Update()? You should create your variables at the top of your script and define them in Start() if they are not going to change.
$$anonymous$$ake a screenshot of inspector of the main camera to show us your animations please.
Answer by clunk47 · Nov 26, 2013 at 08:24 PM
Your animations are not being found because they must be marked as legacy. Go to your project pane and select one of the animations (fpswalk or fpsrun). Then set the inspector to Debug Mode as shown in the photo attached. The animation type will probably be 2. Set it to 1. Repeat this step for your other animation(s).
Also, just for some debugging purposes, and cleaner code, try this as far as your script goes.
#pragma strict
private var childObject : GameObject;
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var height: float; // initial height
var h : float;
var speed : float;
var lastHeight : float;
var childAnim : Animation;
function Start()
{
childObject = Camera.main.gameObject;
childAnim = childObject.animation;
chMotor = GetComponent(CharacterMotor);
ch = GetComponent(CharacterController);
height = ch.height;
h = height;
speed = walkSpeed;
}
function Update()
{
if(childAnim.GetClip("fpsrun") && childAnim.GetClip("fpswalk"))
{
if (ch.isGrounded && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
speed = runSpeed;
childObject.animation.CrossFade("fpsrun", 0.25f);
}
else
childObject.animation.CrossFade("fpswalk", 0.25f);
}
else print("Animations not found.");
if (Input.GetKey(KeyCode.C))
{ // press C to crouch
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
lastHeight = ch.height; // crouch/stand up smoothly
transform.position.y += (ch.height-lastHeight)/2; // fix vertical position
}
Yeah, it removes the "jumpy" transition from fpswalk to fpsrun, looks way better than it was when it was animation.play!
Well, for example, if you're using W and S or Up and Down Arrows (Vertical Axis) to walk, you could do something like this:
if(Input.GetAxis("Vertical") != 0)
{
if (ch.isGrounded && Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftShift) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightShift))
{
speed = runSpeed;
childObject.animation.CrossFade("fpsrun", 0.25f);
}
else
{
speed = walkSpeed;
childObject.animation.CrossFade("fpswalk", 0.25f);
}
}
Answer by phreakhead · Sep 10, 2017 at 03:20 AM
If you don't want to mark your animations as legacy, you can use the new Playables API to play non-legacy AnimationClips:
private List<PlayableGraph> graphs = new List<PlayableGraph> ();
// Just call this function when you want to play an AnimationClip on a specific GameObject.
// from https://docs.unity3d.com/Manual/Playables-Examples.html
private PlayableGraph playAnim(AnimationClip clip, GameObject obj) {
PlayableGraph playableGraph;
AnimationPlayableUtilities.PlayClip(obj.AddComponent<Animator>(), clip, out playableGraph);
// save all graphs we create and destroy them at the end of our scene.
// you might need to optimize this if you make a lot of animations.
graphs.Add (playableGraph);
return playableGraph;
}
void OnDisable() {
foreach (var g in graphs) {
g.Destroy();
}
graphs.Clear ();
}
Answer by SharkoFR · Dec 04, 2016 at 08:33 PM
Hi now we just need check the box legacy in debug mode
when changed to legacy, my animation inspector is all grayed out.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
RayCasting + Animation Problem 0 Answers
The AnimationClip 'Attack' used by the Animation component 'Knife' must be marked as Legacy. 1 Answer
Animation error please help me! 1 Answer
The AnimationClip 'Running' used by the AnimationComponent 'corpse' must be marked as Legacy? 1 Answer