简体   繁体   English

统一动画之间切换的延迟 3d

[英]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.我正在尝试在喷气发动机动画之间切换,但 animation 等待完成当前正在运行的 animation 然后它运行 animation 单击第二个。 Is there any way to fast forward current animation and switch to second immediately.有没有办法快进电流 animation 并立即切换到第二个。 below is the code on jet engine object.下面是喷气发动机 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.喷气发动机和动画师 controller 的图像附在下面。

jet engine喷气发动机

animator controller动画师 controller

Please help me to resolve the problem.请帮我解决这个问题。 Thank you in advance.先感谢您。 Ranjith兰吉斯

Just click the transition line between animations in Animator window, then uncheck the has exit time checkbox.只需在 Animator window 中单击动画之间的过渡线,然后取消选中 has exit time 复选框。 在此处输入图像描述

And also your transitions are linked each other, if you want to trigger different animation immediately, you can use transition from Any State to your desired state. Therefore animation directly play when paremeter fits your condition.而且你的转换是相互链接的,如果你想立即触发不同的 animation,你可以使用从任何 State 到你想要的 state 的转换。因此 animation 在参数适合你的条件时直接播放。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM