- Home /
the animations on my character are messed up
my character has four animations, but most don't work properly. first, the walking animation only works when the character walks back (Presses S) and the running animation only works when the character runs to the right (Presses D). the idle animation is fine and the death animation up until an eight of the way and then it loops through the next couple of frames until the animation ends. as it is only an eighth of the way no matter what the length is, i just paste the last farm a good 25 seconds, and it plays the beginning fine and the end of it is the same so.. it's not a proper fix but it's a fix. here is my script, hopefully someone notices a difference between the keys and their behaviour so i can fix it, or can tell me if it's something else.
using UnityEngine;
using System.Collections;
public class ExperimentalDeath : MonoBehaviour {
public float Health = 100f;
public float LevelResetTimer = 10f;
public Animator DeathAnim;
public AudioClip DeathSound;
public bool isDead;
private float timer;
public GameObject Poof;
public float maxHealth = 100f;
public Animator Walk;
//public Animator Run;
public float Speed;
private Animator Fire;
private bool paused = false;
// Use this for initialization
void Start () {
}
void Dying () {
isDead = true;
DeathAnim.SetBool("isDead", true);
AudioSource.PlayClipAtPoint(DeathSound, transform.position);
}
void Dead () {
Debug.Log("Player Has Died");
Destroy(gameObject, 3);
if(Random.Range(0f, 20f)< 1){
Instantiate (Poof, transform.position, Quaternion.Euler(0f, 0f, Random.Range(0f, 360f)));
}
}
void LevelReset () {
timer =+ Time.deltaTime;
if(timer >= LevelResetTimer){
}
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Escape))
{
paused = !paused;
}
//Begin Walking
if (Input.GetKey("w")&& isDead == false && paused == false){
Walk.SetBool("Walk", true);
transform.Translate(Vector3.forward * Speed);
} else {
Walk.SetBool("Walk", false);
}
if (Input.GetKey("a")&& isDead == false && paused == false){
transform.Translate(Vector3.left * Speed);
Walk.SetBool("Walk", true);
} else {
Walk.SetBool("Walk", false);
}
if (Input.GetKey("d")&& isDead == false && paused == false){
transform.Translate(Vector3.right * Speed);
Walk.SetBool("Walk", true);
} else {
Walk.SetBool("Walk", false);
}
if (Input.GetKey("s")&& isDead == false && paused == false){
transform.Translate(Vector3.back * Speed);
Walk.SetBool("Walk", true);
} else {
Walk.SetBool("Walk", false);
}
//Running
if (Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift) && isDead == false && paused == false){
Walk.SetBool("Run", true);
transform.Translate(Vector3.forward * Speed);
} else {
Walk.SetBool("Run", false);
}
if (Input.GetKey("a") && Input.GetKey(KeyCode.LeftShift) && isDead == false && paused == false){
Walk.SetBool("Run", true);
transform.Translate(Vector3.left * Speed);
} else {
Walk.SetBool("Run", false);
}
if (Input.GetKey("s") && Input.GetKey(KeyCode.LeftShift) && isDead == false && paused == false){
transform.Translate(Vector3.back * Speed);
Walk.SetBool("Run", true);
} else {
Walk.SetBool("Run", false);
}
if (Input.GetKey("d") && Input.GetKey(KeyCode.LeftShift) && isDead == false && paused == false){
Walk.SetBool("Run", true);
transform.Translate(Vector3.right * Speed);
} else {
Walk.SetBool("Run", false);
}
//End of Walking
if(Health > maxHealth){
Health = maxHealth;
}
if(Health <= 0f) {
if (isDead){
Dead ();
LevelReset ();
isDead = true;
} else {
Dying ();
}
}
if(Health != Health){
Debug.Log (Health);
}
if (Input.GetKey("x")){
DamageChar (1);
}
}
public void DamageChar (float amount) {
Health -= amount;
}
}
Also, the walk animation plays even if shift is pressed (is running). thanks for reading this and i hope you can help me solve this and have a good day!
Update: death animation fixed by setting "can transition to self" to false (never enabled it myself, i swear :D )
Another Update: It appears only the last if Input.GetKey is the one that play the animation. i moved forwards (W) to the bottom and then only forwards played the animation. also, i used a piece of software and other than the obvious (the key pressed and the direction to move/rotate) they are all the same.
Answer by TheShadyColombian · Sep 02, 2014 at 10:14 PM
added line after all the sets of Input.getKey that said
if ((Input.GetKey("w")||Input.GetKey("a")||Input.GetKey("s")||Input.GetKey("d")) && Input.GetKey(KeyCode.LeftShift) && isDead == false && paused == false){
Walk.SetBool("Run", true);
} else {
Walk.SetBool("Run", false);
}
and so if any of the keys is pressed it plays the animation. for walking i added && !Imput.GetKey(KeyCode.LeftShift) because it was tripping between the walking and running.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
How to create animations that can be edited with scripts in runtime? 1 Answer
How can I use parameters value in the animation 1 Answer
End of animation with animator. 0 Answers
Why isn't my gun showing up when I play in maximised mode? (Mechanim Animator) 1 Answer