- Home /
Why do my animations only play once?
Hello, today was my first time using the animator object. With the script I applied, the model goes through its correct animation once and then freezes in the first frame of that animation. I know as a fact, the "Speed" parameter of the animator object is changed correctly; I see it change whenever I press an input button. For example, when using my Walk animation, the model freezes just after completed a full walk cycle. What am I doing wrong? Does this have something to do with the animations themselves? Thanks in advance!
Here's my code: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class FirstPersonController : MonoBehaviour { public Animator animatorComponent; private HashIDs hashComponent;
float walkSpeed = 10;
float gravity = 100;
private float yRot;
private float xRot;
public Camera mainCam;
public CharacterController controller;
public GameObject characterMesh;
float deltaVelocity = 0;
float cameraRotation;
//The following are for the jumpscript
bool canJump;
int xValue;
public int jumpHeight;
GameObject player;
void Start() {
player = GameObject.Find("Player");
canJump = true;
animatorComponent = GameObject.FindGameObjectWithTag("PlayerMesh").GetComponent<Animator>();
hashComponent = GetComponent<HashIDs>();
animatorComponent.SetLayerWeight(1, 1f);
}
void Update(){
Vector3 vertical = transform.TransformDirection(Vector3.forward);
Vector3 horizontal = transform.TransformDirection(Vector3.right);
Vector3 height = transform.TransformDirection(Vector3.up);
walkSpeed = 0;//Updates the character's speed
//basic jumping
if(Input.GetAxis("Jump") > 0 && canJump) {
xValue = -1 * jumpHeight;
StartCoroutine(basicJump());
}
//Receive Mouse Input
if(Input.GetAxis("Mouse X") != 0) {
yRot = Mathf.SmoothDamp(yRot, yRot + Input.GetAxis("Mouse X"), ref deltaVelocity, .03f);
}
if(Input.GetAxis("Mouse Y") != 0) {
xRot -= Input.GetAxis("Mouse Y");
}
Screen.lockCursor = true;//Puts the mouse in the screen's center
transform.rotation = Quaternion.Euler(0, yRot, 0);
//Moves the camera, rotate character
mainCam.transform.rotation = Quaternion.Euler(xRot, yRot, 0);
controller.Move((height * (-gravity) * Time.deltaTime)/5);
if(Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) {
//characterMesh.animation.CrossFade("Run");
if(Input.GetAxis("Vertical") != 0) {
walkSpeed = 10 * Input.GetAxis("Vertical");
controller.Move( (float)(walkSpeed * Time.deltaTime)* vertical);
}
if(Input.GetAxis("Horizontal") != 0) {
walkSpeed = 10 * Input.GetAxis("Horizontal");
controller.Move( (float)(walkSpeed * Time.deltaTime) * horizontal);
}
animatorComponent.SetFloat("Speed", Mathf.Abs(walkSpeed) );
//Moves the actual character
} else {
//characterMesh.animation.CrossFade("Idle");
//This segment assumes that variable 'walkSpeed' is set to zero
animatorComponent.SetFloat("Speed", walkSpeed);
}
}
IEnumerator basicJump() { //NOTE TO SELF: REFINE THIS
canJump = false;
for(var i = 0; i < jumpHeight; i++) {
gravity = (-.05f) * (xValue * xValue);
xValue++;
yield return new WaitForSeconds(.001f);
}
for(var j = jumpHeight; j > 0; j--) {
gravity = (.05f) * (xValue * xValue);
xValue--;
yield return new WaitForSeconds(.001f);
}
yield return new WaitForSeconds(.3f);
canJump = true;
}
}
I know for Input.Get$$anonymous$$ey you need to use Input.Get$$anonymous$$eyDown to repeat...
I don't think the input is the problem, since the values are changed in the Animator window whenever I hold down the input keys.
Answer by 13dnizinski · Jan 04, 2014 at 07:36 AM
Thanks for the help, KuPAfoo! I solved the problem - I had to check a box that said "Loop Time" when I imported. I have no clue what it does, but it fixed my problem!
Your answer
Follow this Question
Related Questions
Get weird issue when change from Mechanim -> Ragdoll 0 Answers
Can animation clips be swapped out of the animator override controller in runtime? 1 Answer
How to change animation clips of an animator state at runtime? Is there a way? 5 Answers
how to make multiple transition from one state? 1 Answer
Overwriting animation parameters play animation while other is already playing? 0 Answers