- Home /
Question by
The_Scootakip · Jun 06, 2018 at 07:42 AM ·
c#animationanimatorspritesanimations
Animations out of sync?
Hello. I'm making a 3D game that uses 2D characters.
The character customization system works with each part of the character being a different sprite with its own script for its own animations.
However, the animations don't seem to always sync up perfectly.
Sometimes they sync up fine, but sometimes, they're horribly desynced.
Below is the shirt code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShirtAnim : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
var localVel = transform.InverseTransformDirection(transform.parent.GetComponent<Rigidbody>().velocity);
if (localVel.z > 0) {
anim.Play("ShirtWalkUp");
}
else if (localVel.z < 0) {
anim.Play("ShirtWalkDown");
}
else if (localVel.x < 0) {
anim.Play("ShirtWalkLeft");
}
else if (localVel.x > 0) {
anim.Play("ShirtWalkRight");
}
else {
if (anim.GetCurrentAnimatorStateInfo(0).IsName("ShirtWalkUp")) {
anim.Play("ShirtIdleUp");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("ShirtWalkDown")) {
anim.Play("ShirtIdleDown");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("ShirtWalkLeft")) {
anim.Play("ShirtIdleLeft");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("ShirtWalkRight")) {
anim.Play("ShirtIdleRight");
}
}
}
}
Body code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBodyAnim : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
var localVel = transform.InverseTransformDirection(transform.parent.GetComponent<Rigidbody>().velocity);
if (localVel.z > 0) {
anim.Play("PlayerWalkUp");
}
else if (localVel.z < 0) {
anim.Play("PlayerWalkDown");
}
else if (localVel.x < 0) {
anim.Play("PlayerWalkLeft");
}
else if (localVel.x > 0) {
anim.Play("PlayerWalkRight");
}
else {
if (anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkUp")) {
anim.Play("PlayerIdleUp");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkDown")) {
anim.Play("PlayerIdleDown");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkLeft")) {
anim.Play("PlayerIdleLeft");
}
else if (anim.GetCurrentAnimatorStateInfo(0).IsName("PlayerWalkRight")) {
anim.Play("PlayerIdleRight");
}
}
}
}
They're both pretty much the same, just swapping out animation names.
I don't think it's an issue with the animations themselves either, since, as I've said, sometimes they're synced fine.
Can anyone help me figure out what's going on? It's been stressing me out a lot and I just want this issue fixed.
Comment