Ranjith
Ranjith

Reputation: 19

Delay in Switching between animations in unity 3d

I am trying to switch between jet engine animations, but animation waits to complete currently running animation and then it runs animation clicked second. Is there any way to fast forward current animation and switch to second immediately. below is the code on jet engine object.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class mainScript : MonoBehaviour
{

Animator first;
bool firstTrig = false;
bool secondTrig = false;
bool thirdTrig = false;
bool fourthTrig = false;

string temp = "";
string tempOld = "";

private TMP_Text m_TextComponent;
private bool hasTextChanged;
int visibleCount = 0;

void Awake()
{
    ////m_TextComponent = gameObject.GetComponent<TMP_Text>();
    m_TextComponent = GameObject.Find("/information").GetComponent<TMP_Text>();
}

void OnEnable()
{
    // Subscribe to event fired when text object has been regenerated.
    TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
}

void OnDisable()
{
    TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
}

// Event received when the text object has changed.
void ON_TEXT_CHANGED(Object obj)
{
    hasTextChanged = true;
}

/* /// <summary> */
/// Method revealing the text one character at a time.
/// </summary>
/// <returns></returns>
IEnumerator RevealCharacters(TMP_Text textComponent)
{
    textComponent.ForceMeshUpdate();

    TMP_TextInfo textInfo = textComponent.textInfo;

    int totalVisibleCharacters = textInfo.characterCount; // Get # of Visible Character in text object
    //int visibleCount = 0;

    while (true)
    {
        if (hasTextChanged)
        {
            totalVisibleCharacters = textInfo.characterCount; // Update visible character count.
            hasTextChanged = false;
        }

        if (visibleCount > totalVisibleCharacters)
        {
            yield return new WaitForSeconds(0.01f);
            //visibleCount = 0;
        }

        textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?

        visibleCount += 1;
        yield return new WaitForSeconds(0.1f);
        yield return null;
    }
}

// Use this for initialization
void Start()
{
    first = GetComponent<Animator>();
    //m_TextComponent = GetComponent<TMP_Text>();

    //TMP_Text m_TextComponent = gameObject.GetComponent(typeof(TMP_Text)) as TMP_Text;
    StartCoroutine(RevealCharacters(m_TextComponent));
}

// Update is called once per frame
void Update()
{

    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log(hit.collider.name);
            temp = hit.collider.name.ToString();

            if (temp == "Component_001")
            {
                Debug.Log("open trig");
                first.SetTrigger("open");
            }
            if (temp == "Fan_half004")
            {
                //Debug.Log(m_TextComponent.ToString());
                m_TextComponent.text = "FAN \nThe fan is the first component in a turbofan. The large spinning " +
                    "fan sucks in large quantities of air. Most blades of the fan are made of titanium. " +
                    "It then speeds this air up and splits it into two parts. One part continues through the core or centre of the engine, " +
                    "where it is acted upon by the other engine components.The second part bypasses the core of the engine." +
                    "It goes through a duct that surrounds the core to the back of the engine where it produces much of the " +
                    "force that propels the aeroplane forward.This cooler air helps to quiet the engine as well as adding thrust to the engine.";
                if (temp != tempOld)
                {
                    visibleCount = 0;
                }
                tempOld = temp;
                if (firstTrig == false)
                {
                    first.ResetTrigger("part1In");
                    Debug.Log("part1out " + tempOld.ToString());
                    first.SetTrigger("part1out");
                    firstTrig = true;
                    return;
                }
                if (firstTrig)
                {
                    first.ResetTrigger("part1out");
                    Debug.Log("part1In " + tempOld.ToString());
                    first.SetTrigger("part1In");
                    firstTrig = false;
                    return;
                }

            }
            if (temp == "Cylinder074")
            {
                m_TextComponent.text = "Compressor \nThe compressor is the first component in the engine core. " +
                    "The compressor is made up of fans with many blades and attached to a shaft." +
                    " The compressor squeezes the air that enters it into progressively smaller " +
                    "areas, resulting in an increase in the air pressure. This results in an " +
                    "increase in the energy potential of the air. The squashed air is forced into " +
                    "the combustion chamber.";
                if (temp != tempOld)
                {
                    visibleCount = 0;
                }
                tempOld = temp;
                if (secondTrig == false)
                {
                    first.ResetTrigger("part2In");
                    secondTrig = true;
                    Debug.Log("part2out " + tempOld.ToString());
                    first.SetTrigger("part2out");
                    return;
                }
                if (secondTrig)
                {
                    first.ResetTrigger("part2out");
                    Debug.Log("part2In " + tempOld.ToString());
                    first.SetTrigger("part2In");
                    secondTrig = false;
                    return;
                }

            }

            if (temp == "Circle055")
            {
                m_TextComponent.text = "Combustor \nIn the combustor, the air is mixed with fuel and then " +
                    "ignited. There are as many as 20 nozzles to spray fuel into the airstream." +
                    " The mixture of air and fuel catches fire. This provides a high temperature, " +
                    "high-energy airflow. The fuel burns with the oxygen in the compressed air, " +
                    "producing hot expanding gases. The inside of the combustor is often made of " +
                    "ceramic materials to provide a heat-resistant chamber. The heat can reach 2000°C " +
                    "or more.";
                if (temp != tempOld)
                {
                    visibleCount = 0;
                }
                tempOld = temp;
                if (thirdTrig == false)
                {
                    first.ResetTrigger("part3In");
                    thirdTrig = true;
                    Debug.Log("part3Out " + tempOld.ToString());
                    first.SetTrigger("part3Out");
                    return;
                }
                if (thirdTrig)
                {
                    first.ResetTrigger("part3Out");
                    Debug.Log("part3In " + tempOld.ToString());
                    first.SetTrigger("part3In");
                    thirdTrig = false;
                    return;
                }

            }
            if (temp == "Object028" || temp == "Object027")
            {
                m_TextComponent.text = "Nozzle \nThe nozzle is the exhaust duct of the engine. This is " +
                    "the engine part which actually produces the thrust for the plane. The " +
                    "energy depleted airflow that passed the turbine, in addition to the colder " +
                    "air that bypassed the engine core, produces a force when exiting the " +
                    "nozzle that acts to propel the engine, and therefore the aeroplane, " +
                    "forward. The combination of the hot air and cold air are expelled and " +
                    "produce an exhaust, which causes a forward thrust. The nozzle may be " +
                    "preceded by a mixer, which combines the high-temperature air coming from " +
                    "the engine core with the lower temperature air that was bypassed in the " +
                    "fan";
                if (temp != tempOld)
                {
                    visibleCount = 0;
                }
                tempOld = temp;
                if (fourthTrig == false)
                {
                    visibleCount = 0;
                    first.ResetTrigger("part4In");
                    fourthTrig = true;
                    Debug.Log("part4Out " + tempOld.ToString());
                    first.SetTrigger("part4Out");
                    return;
                }
                if (fourthTrig)
                {
                    first.ResetTrigger("part4Out");
                    Debug.Log("part4In " + tempOld.ToString());
                    first.SetTrigger("part4In");
                    fourthTrig = false;
                    return;
                }

            }
            //first.SetActive(false);Fan_half004
        }
    }
}

}

images of jet engine and animator controller are attached bellow.

jet engine

animator controller

Please help me to resolve the problem. Thank you in advance. Ranjith

Upvotes: 1

Views: 6873

Answers (1)

Akin Erkan
Akin Erkan

Reputation: 321

Just click the transition line between animations in Animator window, then uncheck the Has Exit Time checkbox.enter image description here

And also your transitions are linked to each other, if you want to trigger different animation immediately, you can use transition from Any State to your desired state. Therefore, animation directly plays when the parameter fits into your condition. enter image description here

Upvotes: 6

Related Questions