How to animate this imported character in Unity? - animation

I imported a zombie character asset from the unity store and it came with one animator controller and two animation scripts. I already have my movement script attached to my zombie player and it moves when I press the arrow keys. I want it to do the run animation while it goes forward.
I added the animator component to my player and fed it with the animator controller which came with the asset. But when I click play I only see the idle standing animation and not the running one. do I need to use additional scripts?

You need to call the methods in the Update method, something like this:
void Update() {
if(Input.GetButtonDown("W")||Input.GetButtonDown("S")){
Run();
}else{
OtherIdle();
}
}

Using input
You need to:
1. Detect input every frame (so in your Update() method).
2. Call the methodRun() to set the animation bool to true.
This will make the Run animation play when the "w" key is pressed if the Animator Controller is set up properly:
void Update () {
// If the forward key is pressed...
if (Input.GetKeyDown("w")){
// Play the Run animation
Run();
}
}
Using speed
Alternatively you could give a new parameter to your animation controller "speed" and set the bool for the animation to play when speed is above a certain value. Else if the speed is below the value, play the Idle animation. This way, if you have a Walk animation, you can play it when the speed is between running speed and stationary.

Related

How I can open a door with animations?

I have this script:
Ray ray = new Ray (cam.transform.position, cam.transform.forward);
RaycastHit hit;
Debug.DrawRay (transform.position, ray.direction * 50f);
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast (ray, out hit, 50.0f)) {
if (hit.collider.gameObject.tag == "Door") {
Debug.Log ("YEAH");
}
}
}
How I can start animation open door?
There are a few things that you need to know before you can animate that door.
There are multiple ways to animate an object in Unity. For simple things like that you could decide to just rotate the object (but you need to understand that you will need a Coroutine or a Mathf.MoveTowards or Mathf.Lerp method to avoid instant moving when you call everything in the Update).
But you can also use an animation made by someone else in applications like Blender.
Or finally create an animation with the Unity in game editor and create an Animator to animate your door.
I think that you should use this way.
First follow this official manual:
https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html
to create the clip to open your door.
It's really intuitive and you don't even need to code.
After that you should create an Animator Controller:
https://docs.unity3d.com/Manual/class-AnimatorController.html
You will than create a new state with your animation and you will create a connection between the initial state to your "Open door" animation.
After that you just create a simple bool (in your Animator Controller).
And you will add
this.GetComponent().SetBool("nameofyourboolintheanimatorcontroller", true);
to your script (of course this is valid only if your Animator Controller is in the object assigned to your script... if not you should create a new Animator variable and assign it (for example you could make it public and assign it in the editor).

Unity2D: MissingComponentException: There is no 'Animation' attached to the "Arm" game object

I am trying to make an animation occur every time the space bar is pressed using this code:
using UnityEngine;
using System.Collections;
public class ArmSwipe : MonoBehaviour {
Animation me;
void Start (){
me=GetComponent<Animation>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
me.Play();
}
}
}
Every time I run it i get this error
MissingComponentException: There is no 'Animation' attached to the "Arm" game object, but a script is trying to access it.
You probably need to add a Animation to the game object "Arm". Or your script needs to check if the component is attached before using it.
UnityEngine.Animation.Play (PlayMode mode)
UnityEngine.Animation.Play ()
ArmSwipe.Update () (at Assets/Scripts/ArmSwipe.cs:18)
EDIT:
Your GameObject Arm needs to have an Animation attached to it. It appears that you don't have such an Animation attached to the Arm. Can you attach a screenshot of your scene graph, and your Arm object's inspector properties?
Also, you probably should use Input.GetKeyUp instead of KeyDown like you currently have. KeyDown will simply cause the animation to re-play on each update, and it will look like it's frozen or stuttering.

Unity apply idle animation to model

I created a model in Blender and posed it the way I want it to look. The character will never move and no animations will ever change.
I exported the character with animations to an FBX and imported it into Unity 3D. After dropping the character into the scene, the character's pose is back to the default take.
How can I force the character to use the pose that I want? Do I have to do it in script or is there a setting that I can change to make it play the idle animation at all times?
Putting your model in the scene will not invoke idle animation, animation and rendering is two separate thing. To always play a certain animation on a model you have three option -
1. Via Legacy Animation -
Select you model in the scene, remove the default Animator component and add a new Animation component. (Animator and Animation are two different component type.) It has an option of selecting an animation clip. Drop your animation clip there and choose 'Play Automatically'.
2. Via Script -
You can add Animation component, attach multiple animation and select which one to play via script too.
You can also do this for meca-anim way which is explained in the next point.
3. Via Meca-Anime
Every model has a default Animator component. It needs an animation controller object to run. Create an animation controller (Click on create button in project view). Attach the controller to the Animator component. Double click on the controller to open Animator view. Drop your clip there and make it as default. The default animation plays automatically now. Using Meca-anime has many advantage over legacy way, which is beyond the scope of this answer. Check the following link instead -
Mecanim vs Legacy Animation

how to stop an animation but make it finish the current animation

I make a animation and make it runs it forever. And now I want to stop it after clicking a button. Then animation is rotating a image of a button from 0 to 360 degree.
I'd like to stop the animation but make the animation to finish the current cycle. For example, if the button is rotated at 200 degree, and I click the stop button, I want that the animation still runs to reach 360 degree and then stop.
The animation code is like below:
CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim2.fromValue = [NSNumber numberWithFloat:0];
anim2.toValue = [NSNumber numberWithFloat:-((360*M_PI)/180)];
anim2.repeatCount = HUGE_VALF;
anim2.duration = 10;
[self.imgBtn.layer addAnimation:anim2 forKey:#"transform"];
Thanks!
While you can get the animation from the layer using animationForKey:, you cannot modify the animation that way. As the documentation explains it:
Discussion
Attempting to modify any properties of the returned object will result in undefined behavior.
What you instead have to do
You have to remove the infinite animation and add another animation that makes the final end animation.
Use the presentation layer to determine the current rotation of your layer.
Remove the infinite animation
Add a new animation from the value you got from the presentation layer to the end state.
How to get the value from the presentation layer.
During animation your model doesn't change (when using Core Animation). Instead what you see on screen is the layers presentation layer. You can get then read the current value from the presentation layer just as if it was any other layer. In your case you could get the rotation using [myLayer valueForKey:#"transform.rotation"];
Alternately, you can create a single animation with a completion method that triggers the animation again, in an infinite loop.
Have the completion routine check a flag before submitting a repeat.
If the flag is false, don't trigger the animation again.
Then have your button action simply set the repeat flag to false. The current animation will run to completion, and then it won't be repeated.

XNA Displaying splash screen while content is loading

I'm trying to include splash screen into my XNA project while content is loading. I tried few tutorials but none has helped me. I was trying load content in update method, forcing draw method etc. Since I'm doing my project on Windows PC only I'm looking for a solution to display splash screen (propably animated) in the center of desktop. When content is loaded splash screen should disappear and the game screen should pop up.
I'm looking for some advices and tips how do it since I can't find any help on the Internet.
Personally I was thinking about using Forms but I don't know it well to use it properly.
Thanks in advance.
Since you are loading content, you are probably making a method call that spans over many frames. If you make that call on the GUI thread, you will freeze the game, which isn't what you want.
Instead, call the method inside a Task, and set your game's state as 'loading', and when the task is done, set a state that indicates the task is done.
Then, in your drawing method, you can do something like this:
if (state == State.Loading)
// draw loading screen
else if (State == .....
Extending on what Kendall Fray said, You could use game states like so:
public static GameState currentGameState = GameState.Loading;
public enum GameState
{
Loading, Whatever
}
and on Update or draw:
switch (currentGameState)
{
case (GameState.Loading):
//draw load screen
break;
case (GameState.Whatever):
break;
}

Resources