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 ;)
Related
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'm new to Unity3D and looking for some basic information.
I'm used to OO programming, but I cannot quite see how to access the objects from script.
I created an object, made it a prefab (plan on using it many times) and the object has text on it.
The text uses a Text Mesh. I'm using C#.
How do I thru code, simple example on start, instantiate a new prefab object called Tile at 0,0?
How do you access the text Mesh part of the object to change the text?
I sure there is something simple I'm not picking up on.
Just having trouble understanding how to connect the code to the objects and vice versa.
Updated:
Also wanted to note, I'm trying to load multiple objects at the start, if that makes a difference in the answers.
Update2:
Just wanted to explain a little more what info I was missing when tying the mono code to the unity interface.
In Unity:
Created any object and turn it into a prefab.
Created a second empty game object, place it somewhere in the play view area.
Created a script on the Empty game object.
In Mono code editor:
Created 2 public variables (C#)
public GameObject spawnObj;
public GameObject spawnPoint;
void Update () {
Instantiate (this.spawnObj, this.spawnPoint.transform.position, this.spawnPoint.transform.rotation);
}
Back in Unity:
Select the Empty Game Object.
In the script Component, you should see the 2 vars.
Drag your Prefab Object into the var spawnObj.
Drag the Empty game object into the var spawnPoint.
I did this is the Update, not to smart, but it spawned a cube or 2 or more, spawning from code is all I wanted to understand.
AD1: It's GameObject.Instantiate:
var go=GameObject.Instantiate(prefab, Vector3.zero, Quaternion.Identity) as GameObject;
A prefab is an asset-like GameObject that is used as a template for scene GameObjects.
To use a prefab you have to drag a GameObject to the project window and reference it in code in one of two ways:
Using resources, var prefab=Resources.Load("my-prefab") will load the project file Resources/my-prefab. Note the use of the special "Resources" directory - it's a magic name that is required.
Using a reference, in a MonoBehaviour add a public GameObject prefab field. Then on a GameObject using this class you can drag and drop your prefab from the project window to create a reference to it. You can then use the prefab field in your code. No "Resources" directory needed here.
Prefer option 2, as all resources are in the final binary, while normal assets are trimmed when possible.
AD2: get the TextMesh compontent and modify text:
go.GetComponent<TextMesh>().text="new text";
Im making a japanese educational game and made this sprite sheet. Ive already sliced it in the sprite editor and named all the symbols.
Im instantiating 5 prefabs that need to have 5 different sprites with different
symbols. I thought I was gonna be able to either assign the entire sheet to a game object or just keep adding sprite renderers to the game object, but I was wrong.
How can I achieve this?
You can change the sprite directly from code.
this.GetComponent<SpriteRenderer>().sprite = someSprite;
You can dependency inject the sprites themselves using the editor and a public variable for each; or if you prefer, you can use Resources.Load instead.
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.