• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by daawaa · Sep 14, 2013 at 11:10 AM · animationrigging

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by OperationDogBird · Sep 14, 2013 at 08:55 PM

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! :P

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daawaa · Sep 15, 2013 at 08:58 AM 0
Share

Thanks a million for the in depth response there! Very helpful!

I will test these things out that you've suggested and get back to let you know what I did.

Cheers! D

avatar image daawaa · Sep 15, 2013 at 11:27 AM 0
Share

I understand what you are saying bout switching meshes would be heavy and quite a bit complex.

Would you know how this would compare with say an animated texture (placed on a plane) of a run cycle in terms of heaviness?

avatar image OperationDogBird · Sep 18, 2013 at 06:22 AM 0
Share

Sorry for the late response. Ill try to provide some of my thinking here... Warning! Non-Animation-Guy Rambling To Follow !

If it is a full 2d game i would use a sprite atlas and traverse the 'frames/sprites', for 2.5d i would do a standard animation. I do not know how animation keys work behind the scenes, but i would guess that only the key frames that represent an actual change would be used/process/computed(however you want to think about it). I am probably as far from an animation guy as you can get, but the memory usage with a standard animation would(in my $$anonymous$$d at least) outshine any mesh swap you do.

However, this method is kind of cool if you want to dynamically change the animation, ie. randomized animation (in a simple-ish way). Heres a down and dirty explanation of what i mean:

Lets say you have 8 different meshes for each state of the leg. You could then load/swap a random mesh(based on your algorithm) for the current leg state. Thus you can actually make it look more real than if you used a standard single linear animation set. $$anonymous$$aybe the leg bent in a bit more this time, or the ankle rolled a little bit, etc.. This will get heavy depending on the rate you swap out meshes, but at lower intervals you, and more importantly your players, will not notice.

This is only my opinion, as again i am no animation expert. This might actually be a cool topic for a forum post to get some insight from the community and actual animators.

avatar image daawaa · Sep 18, 2013 at 10:06 AM 0
Share

Thanks for that input!

This is a proof of concept test for an animation studio and they requested that we don't use any sprite sheets or rendered animated sequence (from flash for example) due to the high amount of assets and animation needed for game. Therefore we are trying to create this whole project using $$anonymous$$ax (modelling and animation) and unity.

The animation is quite simplistic and cartoon like where animation of the characters are quite snappy/poppy. We are going to approach it like cutout animation practice (sort of like how the first south park season was done) where we will be using multiple layers that will then look combined when viewed orthographically.

I've done a quick test with this principle in $$anonymous$$d for the character blinks that seems to work and will probably be using this method. We are going to hide the animated symbols behind the background plane so it is not visible. Then through controllers in $$anonymous$$ax the animator can dial up different expressions where a simple rig pops the eye shapes to the front of the character. When the animations are complete in $$anonymous$$ax we will then import into Unity and hookup to controls.

So far it's looking promising :)

avatar image mr_pablo · Nov 27, 2014 at 11:47 AM 0
Share

I'm doing something similar, using a script that was posted on here. It has an array that needs manually populating via drag-n-drop (like your example) but I'd like to know if its possible to populate the array with script, so I can click a button and the meshes i have in the resource folder as added to the array?

avatar image
0

Answer by daawaa · Sep 24, 2013 at 10:41 AM

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is it possible for two models to share one set of bones? 1 Answer

PlayQueued doesn't work inside a for loop? 0 Answers

Playing an Animation that is within a GameObject 1 Answer

Why won't it run 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges