Mesh Swap or Switch on keydown

Hi,

I’m doing a test for a 2D platform game and was wondering if anyone would have some advice with a procedure I want to try and implement.

I want to create a substitution based animation for a main character, where I will create 6 separate mesh shapes of the character leg that will cycle through a walk motion and make unity run through these shapes in a loop creating the illusion of animation.

I was hoping there would be a way of creating a scripted procedure where if the left arrow key was pressed it would loop through these meshes that would then be linked to a parent control object for universal movement.

I’d love to hear opinions on the possibility of doing something like this.

Many Thanks!

D

This doesnt sound like a good approach. Why not just create an animation in your modeling program? I have a feeling that switching out meshes constantly will be heavy, and honestly a lot more work in the end for you. But if you have your heart set on this method for whatever reason, a basic rundown will be this:

Create an array of type Mesh. Populate the array in the inspector.

Create a global int variable to track the current mesh index.

Create a function that will take the input ( maybe a bool, positive is true, negative is false) //ie. left arrow vs right arrow

Each time this function is called you increase or decrease you global index respectively.

Make sure the index is within the bounds of the array.

Finally apply the mesh[currentMeshIndex] to the mesh filter in question.

You may have to alter the collider as well to reflect the current mesh.

Here is the basic idea of the script. I did not test this, so there could be misspellings or other issues, this is more of an example than fully working code.

public Mesh[] meshes = null;

private int meshIndex = 0;
private MeshFilter meshFilter = null; //You will need to set this in Start() or make public

public void MyMeshAnimator(bool stepIsPositive)
{
    if(meshes != null && meshFilter != null) // Null checks
    {
        if(stepIsPositive)
        {
            meshIndex++; //Increase the index
            if(meshIndex == meshes.Length) meshIndex = 0; //Reset the index if out of bounds
        }
        else 
        {
            meshIndex--; //Decrease the index
            if(meshIndex == -1)meshIndex = meshes.Length - 1; //Reset the index if out of bounds
        }
        meshFilter.mesh = meshes[meshIndex];
    }
}

Really the only thing missing here is the rate at which you change the mesh. Currently if you call this from update everyframe, it will go crazy! :stuck_out_tongue:

I also suspect you may do this for more than just the legs, if so you will want to maybe create an enum that determines what animation mesh needs changing, then apply that logic to the above function. Happy coding.

The proof of concept has been completed and the method mentioned has done the job!

All the animation was done in Max where I rigged the substitution of character expressions to attributes for the animator to control. This was done using the method (described in a reply) where the mesh is skinned to a bone that is positioned behind the BG and is called to the front, “stage area” via scripted expressions.

Looks well …pity I can’t show it to you :slight_smile: