- Home /
Model Animation with an Array
I'm working on a project where I have a series of models that I'm trying to animate using an array. There are 12 models total. The idea is that when I set each model on/off in the game environment as I iterate through the array, it will give the illusion of animation. The reason why I'm doing this is because I have no real experience with actual animation techniques, and this series of models is what I was provided with. I figured that this might be the best that I could do, but is this idea even feasible to begin with? Any insight on this would be greatly appreciated. I also have the code that I've written for the script I've been working on as well. Hopefully the logic for the AnimateModel method is sound, but if not please let me know. I hope that I've made both my problem and code clear enough. If not I would be happy to try and offer further clarity. Thanks.
EDIT: As I stated in a comment below, unfortunately something is not working correctly. I'm not getting the desired result. When I run the game all of the models seem to appear together at the same time. Once I stop the game all of the models are then turned off as well. There's no hint of the proper animation during runtime that I'm looking for. When I call the AnimateModel method in update it causes Unity to crash as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelUpdate : MonoBehaviour
{
//Public and private objects/variables
public GameObject[] models = new GameObject[12];
public bool running = true;
private bool firstPass = true;
private int i = 0;
//LoadArray iterates through the models array, instantiating GameObjects
void LoadArray()
{
for (int i = 0; i < models.Length; i++)
{
models[i] = new GameObject();
}
}
// Use this for initialization
// Turning all of the models off to start with
void Start()
{
for(int i = 0; i < models.Length; i++)
{
models[i].SetActive(false);
}
}
/*
* The primary method for iterating through the array. If during the first pass i == 0, don't bother with turning the previous model off.
* If i == 0 and it's no longer the first pass, turn off model 11 and turn model 1 back on.
* If i == 11, turn off the model that preceded it, turn model 11 on, set firstPass to false as it's no longer the first pass, and set i == 0 to restart the loop.
* Otherwise iterate through the array, turning off the previous model first and turning the current model on to give the appearance of animation.
* As long as the end of the array has not been reached, it is safe to increment i by one.
*/
void AnimateModel()
{
while(running)
{
if (i == 0 && firstPass)
{
models[i].SetActive(true);
}
else if(i == 0 && !firstPass)
{
models[11].SetActive(false);
models[i].SetActive(true);
}
else if(i == 11)
{
models[i - 1].SetActive(false);
models[i].SetActive(true);
firstPass = false;
i = 0;
}
else
{
models[i - 1].SetActive(false);
models[i].SetActive(true);
}
if(i != 11)
{
i++;
}
}
}
// Update is called once per frame
//Originally AnimateModel() was called here, but this resulted in the program crashing with every run.
void Update()
{
//AnimateModel();
}
}
Are you just looking for a more efficient way to achieve this, or does something about this script not work correctly?
Yes, unfortunately something is not working correctly. I'm not getting the desired result. When I run the game all of the models seem to appear together at the same time. Once I stop the game all of the models are then turned off as well. There's no hint of the proper animation during runtime that I'm looking for. When I call the Animate$$anonymous$$odel method in update it causes Unity to crash as well. Perhaps it's a problem with the Animate$$anonymous$$odel method, or I'm not doing something correctly with Update. I know that calling Animate$$anonymous$$odel by itself in Update may not be entirely wise, but I'm not sure how to get the model to constantly Update within the game without directly tying it to the frame rate.
I'm continuing this with an official answer so anyone else who has a similar problem doesn't have to dig through the comments to find a solution.
Answer by zblase · Apr 07, 2020 at 07:16 AM
If you're activating every object in a while statement, then yes, all of your models will seem to appear at the same time. A while statement loops hundreds of times a second (depending on the complexity of your code inside said while statement). You need to implement something like a coroutine if you want your animations to start at different time intervals (https://docs.unity3d.com/Manual/Coroutines.html). Your models are turning off because you're initializing them in the code. Once the code stops, your models will disappear. The Update method gets called every time there is a new frame, so if you have complex code inside AnimateModel() then your program will crash because said method is repeatedly getting called. I'm not sure what you have in that method so I can't say for sure if that's the problem. Are you using an animation component on the GameObjects? I'm having trouble trying to understand what's going on with AnimateModel() and why you're calling it every frame.
Your answer
Follow this Question
Related Questions
Make mesh collider constantly change 1 Answer
Scale animation with model 1 Answer
Mesh suddenly started imploding while animating. 0 Answers
Can't find Mecanim? 2 Answers