In Unity 3D (2D mode), I have a few Game Objects linked with Animator, and animation made with Animation Timeline. The animation is initially disabled:
public GameObject car;
Animator carAnim;
void Start() {
carAnim = car.GetComponent<Animator>();
carAnim.enabled = false;
}
Then, with an input event, the animation is set to enable:
void Update() {
if(Input.GetKeyDown(0)) {
carAnim.enabled = true;
}
}
However, when the animation starts to play, the whole screen flickers once. How to remove the flicker?
I think the problem is that you're trying to enable and disable Animator component at runtime. Animator was not designed to be used like this. If you want to play animation in certain point of time, just keep Animator enabled, create 2 states - "Idle" and "YourAnimation", create transition between them and control it with some bool parameter.
Much simple way is to use Animation component (if you don't need Mecanim and transitions between states), where you can play animation with just one line of code:
gameObject.GetComponent<Animation>().Play("MyAnimation");
Related
So, I recently learned how to animate certain objects, but I don't know how the code works.
I learned the animator tab, where idle camera animation will transition from walking if I am holding my movement keys. I searched tutorials on youtube and google, but all I can find are animations found on models, not actually on the camera.
If I used that logic on a camera, nothing actually happens, and I don't know what to write.
I've set a bool, which is labeled "WalkingForward", because I have different animations on different movements.
When it is true, the forward walk animation will play, and if false, it turns back to idle animation.
The only thing I can't figure out is a code that will detect the "Horizontal" and "Vertical" movements to start playing the animation.
Here is the link of my animator controller tab https://imgur.com/a/euM9yFl , and I would really appreciate if someone has an idea.
You can handle/detect movement in the Update() method for a GameObject, then get the Animator for the Camera and set the Animator parameters.
See Unit Animation Parameters Docs for more info, but the general idea is something like this:
// Update method attached to wherever you want to detect movement
void Update()
{
// isMovingHorizontally and iMovingVertically are not defined here
// They could either be based off the speed/momentum of the GameObject
// Or the input from the player
var isMovingHorizontally = isMovingHorizontally();
var isMovingVertically = isMovingVertically();
animator.SetBool("MovingHorizontally", fire);
animator.SetBool("MovingVertically", fire);
}
I am trying to make a character play a specific animation when pressing the R button. I would like him to play the whole animation and then stop once it has run a cycle.
The issue now is that when I press "R" the animation only plays for a frame and no animation plays at all.
I started with this statement below.
void Update ()
{
if (Input.GetKeyDown(KeyCode.R))
{
anim.Play("Ready");
}
}
I then read that it might be better to create a bool instead, if the bool is true -> play Animation.
public bool ready;
void Update ()
{
if (Input.GetKeyDown(KeyCode.R))
{
ready = !ready;
}
if (ready == true)
{
anim.Play("Ready");
}
}
When bool ready is true, the animation plays one frame and then the character becomes static. If the bool the gets false again, the character returns to his idle animation.
My latest attempt to making this work is by structuring the code like this:
if (Input.GetKeyDown(KeyCode.R))
{
ready = !ready;
if(ready == true)
{
anim.Play("Ready");
}
}
However this only causes the animation to play a very small amount of frames until going back to the idle animation. Even though ready stays true.
I am not using the "Animator component" but rather the "Animation component" instead. This are the settings of it: Animation Component
Could it be the void Update() causing the issue?
Also, is there a way to make sure the animation only plays for one cycle and then stops?
Your method looks valid. Are there chances that your imported animation isn't?
You should use the Animator as suggested:
drop your clips in the Animator window,
right click on the default state > "make transition", release on the next clip,
create a parameter of type bool named "ready",
assign this parameter in the transition from idle to the next state (select the arrow and click the "+" button at the bottom of the component in the Inspector window, select your parameter),
in your script, you just have to call:
myAnimator.SetBool("ready", true);
There are a lot of benefits of using the Animator :
handle many animation clips, automatic transitions, preview, and more features with "humanoid" rigs (in the model import settings)...
I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation
What i want is when running the game that the animation clip will start playing automatically.
I added to the Main Camera a Animation component.
Added in the Inspector to the Animation the clip file name Camera_Sign003 also set the Animations size to 1 and added there the same clip Camera_Sign003.
Also i set to true the Play Automatically.
I also attached a script to the Main Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraPoints : MonoBehaviour
{
public Animation anim;
IEnumerator Start()
{
anim = GetComponent<Animation>();
anim.Play(anim.clip.name);
yield return new WaitForSeconds(anim.clip.length);
}
}
And i checked with a break point it's getting to the line anim.Play....And the length is 10 but nothing happen after 10 seconds.
The animation is a short clip moving the camera from point to point.
The Main Camera is child under ThirdPersonController.
The Animation component is a legacy component and shouldn't be used unless you need to support a legacy tool or project.
What you really need is an Animator component with an Animation Controller for the object. The easiest way to set this up is by selecting the object you want to animate (e.g. Main Camera), opening the 'Animation' window, and clicking the 'Create' button there.
The new animation created will play automatically and loop by default.
More information can be found here: Unity - Manual: Creating a New Animation Clip
If I have a player and let's say a red ball hits my player and the player goes on fire... OK easy enough I did that. But now I want to add to the code below under the if statement that a blue ball hits my player and freezes or a black ball and causes death animation... But it's just not working out to add multiple animation events to 1 player.
Public GameObject GoneGo;
void OnTriggerEnter2D(Collider2D collisionObject)
{
if (collisionObject.gameObject.tag == "Orb")
{
PlayGone();
}
}
void PlayGone()
{
GameObject gone = (GameObject)Instantiate(GoneGo);
gone.transform.position = transform.position;
}
What you could do is perform a name check on the collider that impacts the player. You already have it checked the tag, have it check for redball,blueball, or blackball as the GameObject name afterwards.
You can handle multiple animations by looking into unitys mechanim system, which is the animator component (not animation component).
https://community.mixamo.com/hc/en-us/articles/203879268-Unity-Mecanim-Animation-Basics
What you would do after setting up the animator is have it SetTrigger for different trigger parameters. Or, you could use something like SetBool with a bool in the parameters like HitByBlueBall or HitByRedBall.
Hope this helps!