I'm trying to create a new animation clip file and set it in Animation component , that will be added to a GameObject in runtime.
something like this:
Animation addedAnim = fbxObject.AddComponent<Animation>() as Animation;
everything I try doesn't work :(
Related
I'm not an expert at unity, so bear with me. I want to load a 3D model and apply an animation to it at runtime by using the file path. They are both FBX files and I've already been able to load the 3D model in the scene using an FBXImporter in the following code.
public GameObject Eve;
GameObject fbx;
public static string fbxPath = /*File path*/;
// Start is called before the first frame update
void Start()
{
if (File.Exists(fbxPath))
{
fbx = ModelImporter.Importer.Import(fbxPath); //Loads the 3D model from the fbx file and makes it a gameObject
fbx.transform.parent = Eve.transform; //parenting that gameObect
}
}
Now I just need to apply the animation from the other fbx file to it but I do not know how to do that. Any Help?
ps. In case you want to replicate it, I got the FBXImporter from this link: https://github.com/yuen33/FBXImporter and I got both the 3d model and animation from mixamo.
First of all simply run it in the editor and then check in the hierarchy what components are on your imported model.
If there is no Animator or Animation component anywhere then your importer doesn't support them at all.
If there is an Animator checkout the AnimatorController of it. There should be states you can switch to via Animator.Play providing according state name.
Or if it is an Animation component you will find a list of clips you can simply Animation.Play
Edit I just tested with this and this model and your importer doesn't seem to support animations ;)
I'm new to Unity and it may be a basic information.
I want to animate a FBX model from another FBX model animation.
For example, We have A_FBX and B_FBX files.
*A_FBX has model and without animation.
*B_FBX has model and animation
I have to animate A_FBX model with B_FBX animation with C# program only.
And there should not be any manual steps. Everything through C# code only.
Tried following code, but didn't work. It has some manual work that is we will map the each game object properties with A_FBX and B_FBX.
Animation anim;
//obj1 is GameObject and it will be mapped to A_FBX
anim = obj1.AddComponent<Animation>();
//obj1 is GameObject and it will be mapped to A_FBX
anim = obj2.GetComponent<Animation>();
anim.Play();
Thanks in advance.
I am working on a 2D game and the text prefab that I instatiate doesn't position itself over the gameObject clicked (which is the goal). I've set the Canvas as parent of the prefab via script after spawning it and it doesnt change position.
// creating hit text
GameObject canvas = GameObject.Find("Canvas");
GameObject hit = (GameObject)Instantiate(hitText, transform.position,Quaternion.identity);
hit.transform.SetParent(canvas.transform, false);
hit.transform.position = transform.position;
P.S: this sample code worked with a text made with the Unity Text Editor. Does that mean TexhMesh Pro won't support this function?
For anyone who might have this problem please understand that I made sure that the instantiated text is a child object of the canvas. The problem was that my text has been changed on start by an attached animation. The script above was working fine but the animation was changing it.
You can easely fix this problem by setting the parent of the text to an empty GameObject, which then serves as a container for the text, allowing the text to change its position relatively to it's parent only.
I'm making 2D platformer game using Unity 4.3.4 engine. I've created a simple prefab, which have two animations: "idle" and "death"(i used "animator") and script to control this animations.
And here's the problem: when i instantiating clones of this prefab, they always show "idle" animation and don't turn on "death" when needed.
pos = new Vector3 (-5, 4, 0) * TileSize;
newObject = Instantiate (Bonus, pos, Quaternion.identity) as GameObject;
But what is insteresting: i found a way to make animation work fine. Just add after instantiation one string like this:
newObject.animation["boxNew"].speed=1;
or this(or any string that trying to operate with "animaton"):
newObject.animation.enabled=true;
Of course i get exception at this string: "MissingComponentException: There is no 'Animation' attached to the "BonusBlock(Clone)" game object" . This is true, i really don't have Animation component, i have Animator. But why everything is working this way? Can anyone explain this?
The animator is really only used if your going full blast with the new mecanim/state flow animator, what you prolly want to do for a simple example would be to add a Animation (not animator) component to your prefab, and assign your 2 animations to the animations list in the inspector for the said animation component. Afterwards on the object you could use newObject.animation.Play("death"); to play the death animation when you want it to trigger. or use something like newObject.animation.CrossFade("death"); for a blended animation.
Found here -> http://docs.unity3d.com/Documentation/ScriptReference/Animation.html
Unity Technologies gave the answer: Before deactivating, use a default state to reset the gameObject. Discussion on Unity3d Forum
If you can read chinese, click here
I need to load one specific animation clip from .fbx file and play it in loop.
I tried this:
GameObject go = Instantiate(Resources.Load("Animations/ubohost")) as GameObject;
ubohost.fbx is file with model and animation clip called mixiamo.com.
Then I am sure that I should add the animation to the gameobject:
Animation anim = go.AddComponent<Animation>();
And after that somehow add a clip, I tried many ways, but none works.
I know that it should be really easy, but I am not getting anywhere.
Basically I need just to load and play one animation with script.