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.
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.
So your animations only consist on moving the camera ? Do you have exactly one object called "Main Camera", and does it contains the animations "fpsrun" and "fpswalk" (I think it is case sensitive, so check it carefully in the inspector) ?
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
}
I was actually doing that but changed it back to play because I wanted to keep the script as close to your original as possible so I could figure out what the Hell was going on lol XD. CrossFade is always better to use, especially in Update(). You can CrossFade smoothly by setting the time to fade: [animation.CrossFade][1]("fpsrun", 0.25f); [1]:http://docs.unity3d.com/Documentation/ScriptReference/Animation.CrossFade.html
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 ();
}
What is ch??? Please post full script so I can troubleshoot.
– clunk47Is the game object which contains this script animated ? or is the childObject animated ?
– KiraSenseiThe childObject is animated, this script is on the parentObject (First Person Controller) and the animations are in the childObject (Main Camera)
– Commander_QuackersSo your animations only consist on moving the camera ? Do you have exactly one object called "Main Camera", and does it contains the animations "fpsrun" and "fpswalk" (I think it is case sensitive, so check it carefully in the inspector) ?
– KiraSenseiYes, thats exactly how it is
– Commander_Quackers