Untitled

 avatar
unknown
csharp
4 years ago
2.0 kB
21
Indexable
///In VoxelAnimation.cs ------------------------------------------------------------------


using UnityEngine;

[CreateAssetMenu(order = 51)]
public class VoxelAnimation : ScriptableObject
{
    [Tooltip("Animation speed. 10.0f = 10 frames per second.")]
    public float speed = 10.0f;

    [Tooltip("Animation frames")]
    public Mesh[] frames;
}






///In VoxelAnimator.cs ------------------------------------------------------------------


using UnityEngine;

public class VoxelAnimator : MonoBehaviour
{
    //Bool for testing the script
    public bool walking;


    //Components
    [Header("Components")]
    public MeshFilter body;

    [Header("Animations")]
    public VoxelAnimation idle;
    public VoxelAnimation walk;

    //Runtime
    private float animationTime;
    private VoxelAnimation current;             //The animation that is currently played


    private void Update()
    {
        //Changes the animation according to the boolean walking.
        if (walking)
        {
            if (current != walk)
                OnAnimationChange(walk);
        }
        else
        {
            if (current != idle)
                OnAnimationChange(idle);
        }

        //Play the current animation.
        Animate();
    }

    private void OnAnimationChange(VoxelAnimation newAnimation)
    {
        current = newAnimation;
        animationTime = 0.0f;                                                   //Reset the animation time. The new animation starts on its first frame.
    }

    private void Animate()
    {
        animationTime += Time.deltaTime * current.speed;                        //Advances the animation time at the expected speed
        int frame = Mathf.FloorToInt(animationTime) % current.frames.Length;    //Give the corresponding frame of the animation
        body.sharedMesh = current.frames[frame];                                //Apply the corresponding mesh for this frame
    }
}
Editor is loading...