Playing two separate animations one after another upon completion - c#

I'm trying to play two animations one after another in unity (each animation is two separate paths for the main camera) and from my research I think I need to use the following:
// Update is called once per frame
void Update () {
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
However, with this code attached to my camera, my game still only plays the first path. I created the paths so that there should be half a second delay between the different paths. I have both animations in the animation component of my camera (see attached pics) and I'm stumped as to why this isn't working.
Am I missing something?

As stated by user2025312, you are calling the code each frame. Which means that each frame you're telling it to play the first animation immediately, and you queue the second one. That won't work.
Depending on what you want to do you have several options. Say you want to continuously play the two animations in a loop. What you could do is:
void Update () {
if (!animation.isPlaying)
{
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
}
As should be easy to follow, that checks whether or not there is an animation playing, and if not, it will queue the two, playing the first one immediately. That will work fine within Update().
If however you're looking to play the animation sequence just once (or upon a trigger) place the two lines within Start() or a method of your choice.
void Start () {
animation.PlayQueued("CameraMovement", QueueMode.PlayNow);
animation.PlayQueued("SpaceSceneMovement", QueueMode.CompleteOthers);
}
That should work just fine. If not, verify that your animations work individually.

Related

How can I make two animations work together in unity?

I'm making a first person shooter and im trying to make a reload animation.
I want to make it look like the hand grabs the mag and pulls it down.
How do I make two animations work together?
I remember having to do that for my wrestling game. What I did was create 2 controllers and 2 animations. Controllers should be the same (have same timing, transition and all that). Then make one animation for example animation of how hand reloads a gun. Now memorize sample rate and in which frame you put keys and what do they do (what do they change). Now start animating how mag is getting pulled BASED on what you have done previously. Like in what frame it should go down and in which frame it should be thrown or new one should be inserted and etc.
Then you will have to enter play mode in order to see them simultaneously. I am making a wrestling game and this method is what I use.
Good luck
https://docs.unity3d.com/Manual/AnimatorOverrideController.html
he Animator Override Controller is a type of asset which allows you to extend an existing Animator Controller
, replacing the specific animations used but otherwise retaining the original’s structure, parameters and logic.
This allows you to create multiple variants of the same basic state machine
, but with each using different sets of animations. For example, your game may have a variety of NPC types living in the world, but each type (goblin, ogre, elf, etc) has their own unique animations for walking, idling, sitting, etc.
By creating one “base” Animator Controller containing the logic for all NPC types, you could then create an override for each type and drop in their respective animation files.

Strange plane behavior in Unity3D while changing scene

I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.

Activate and Deactivate Invoke function

I attempted to give a delay to my function in Unity using C# language. By using Invoke, I achieved to add delay when the cursor is moved to a certain area. However, now I need to cancel the Invoke when the cursor is moved from that certain area. The idea is, when I accidentally move the cursor to the area, I can cancel whatever function being invoked there.
Here's my current code.
void SceneCall(){
Application.LoadLevel (MainMenu);
}
void SceneCallIn(){
Invoke ("SceneCall", 3f);
}
if (_newGameButton.Contains (Event.current.mousePosition)) {
SceneCallIn();
}
I tried to use CancelInvoke() as else, and it even doesn't want to invoke anymore when I move my cursor to _newGameButton afterwards. How do you cancel an Invoke then activate it once again? Thank you for your answer~
EDIT: Like #Programmer says in comments, what I exactly want to do is when my mouse is position at a certain point, timer will start counting. After 3 seconds
MainMenu scene will be loaded. While the timer is counting and the pointer is moved away from that certain position, I want the timer to stop and I don't want the MainMenu scene to be loaded anymore.
If you truly want to have "rollovers" and so on, here is a full tutorial:
https://stackoverflow.com/a/36046495/294884
Really you will have to become totally expert in all that, before you can do it.
I must tell you again though:
For 15 years now, you should never use rollovers for any reason in interface.
I faced the same problem yesterday, but I solved simply calling again the invoke method.
Here is my code:
public void OnGazeEnter()
{
InvokeRepeating("OnGazeStay", 0.0f, 1.0f / 30.0f);
}
public void OnGazeExit()
{
CancelInvoke("OnGazeStay");
}
In your case, if you want to register more than one method, you will have to save some sort of List<String> myMethods, so you will be able to delete every method that you've registered.

Non-legacy animation cannot be stopped / controlled

I made an animation using Animation panel, which swaps images from time to time. Read from the Internet, this is not a legacy animation.
Here is the Animation panel screenshot:
Then, I add the Animation and Animator components to the Game Object and assign the animation, which is called Animation01, to it. Here is the screenshot from Inspector of the Game Object:
I try to use the following C# script to stop the animation :
using UnityEngine;
using System.Collections;
public class Scene1 : MonoBehaviour {
public GameObject ball;
// Use this for initialization
void Start () {
ball.animation.Stop();
}
// Update is called once per frame
void Update () {
}
}
but the animation didn't stop. It prompts a notice in Console:
Default clip could not be found in attached animations list.
What did I miss?
UPDATE: By disabling/ removing Animator component, the animation is stopped and cannot be controlled by codes. I need to control the animation by codes.
Unity had implemented two animation systems throughout it's history. Animation component belongs to the legacy animation system, while Animator component belongs to the new animation system. So, adding both components makes no sense: you either use the old system or the new.
The main difference between the legacy and new animation system is that the new animation system is much more advanced in way it's controlled. However, it also means that it's more complicated. It adds another level of abstraction: instead of launching animation yourself, you control variables that influence the behaviour of a special state machine, animation controller.
So, if you want to use animations for something really, really simple, where you want just to launch animations manually, it may be better to use legacy animation system instead. But the components are not the only thing that is different: the animation files themselves are marked to determine if they are "legacy" or not. By default, when you create an animation in the new unity version, it belongs to the new animation system. If you want to use it with a legacy animation, you have to mark it as a legacy animation. Unfortunately, you'll have to do a little hack to achieve that.

Animator Controller: Animation Completion Callback

I've seen some posts on the legacy system using Animations, but I'm using an Animator Controller and I want to know when an animation has completed playing.
Is there a way to assign a callback to when an animation has completed playing?
I could create a co-routine and check every frame but I feel thats the wrong way to do it.
Do you know how to check when an Animation Controller Animation has finished playing?
Since you are using Mecanim this will give you what you want:
if(this.animator.GetCurrentAnimatorStateInfo(0).IsName("AnimationName"))
{
// Do something.
}
Make sure you use the correct AnimationName and you will be alright.
The API reference GetCurrentAnimatorStateInfo.
Gets the current State information on a specified AnimatorController
layer.
It takes one parameter which is The Layer's Index.
Event Solution
You can trigger an Event by calling a function. You need to write the name of the function you want to call at the selected frame in the Function area of the Edit Animation Event window.
Short tutorial on how to do it.

Categories