public void StartMotion(Live2DMotion rawMotionData)
 {
     rawMotionData.setFadeIn(motionFadeSpeed);
     rawMotionData.setFadeOut(motionFadeSpeed);
     l2dMotionManager.startMotionPrio(rawMotionData, 0);
     isBreath = false;
 }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        int motionIndex = 0;

        // 根据返回的情感指数,在对应情感动作中随机选取
        if (ChatWithTuling.flag == true)
        {
            rate = ChatWithTuling.rate;
            // 积极动作
            if (rate > 0.5)
            {
                motionIndex = optimisticIndex[getRandomIndex(optimisticIndex.Length)];
            }
            // 消极动作
            else if (rate < 0)
            {
                motionIndex = pessimisticIndex[getRandomIndex(pessimisticIndex.Length)];
            }
            // 中立动作
            else
            {
                motionIndex = neutralIndex[getRandomIndex(neutralIndex.Length)];
            }

            print("rate: " + rate + ", motionIndex: " + motionIndex);

            ChatWithTuling.flag = false;
        }


        //为模型设置用于显示的画布,后面是2个矩阵相乘
        live2DModel.setMatrix(transform.localToWorldMatrix * live2DCancasPos);

        //设置物理过程,更新模型的参数
        physics.updateParam(live2DModel);

        //设置眨眼动作,设置模型的参数
        eyeBlinkMotion.setParam(live2DModel);

        //按M键切换动作并播放声音
        if (motionManager.isFinished())                         //动作完成,切换到默认的动作
        {
            motionManager.startMotionPrio(motions[0], 1);       //默认的动作的优先级为1,数值较高,优先级较大
        }
        // 展示特定情绪的动作
        else if (motionIndex != 0)
        {
            motionManager.startMotionPrio(motions[motionIndex], 2);     //新动作的优先级为2
        }

        ////按M键切换动作并播放声音
        //if (motionManager.isFinished())                         //动作完成,切换到默认的动作
        //{
        //    motionManager.startMotionPrio(motions[0], 1);       //默认的动作的优先级为1,数值较高,优先级较大
        //}
        //else if (Input.GetKeyDown(KeyCode.M))
        //{
        //    motionManager.startMotionPrio(motions[motionIndex], 2);     //新动作的优先级为2
        //    //motionIndex++;

        //    print("motion index: " + motionIndex + "\n");

        //    if (motionIndex >= motions.Length)
        //    {
        //        motionIndex = 0;
        //    }


        //    //播放声音
        //    audioSource.clip = audioClips[audioIndex];
        //    audioSource.Play();

        //    audioIndex++;
        //    if (audioIndex >= audioClips.Length)
        //    {
        //        audioIndex = 0;
        //    }

        //}
        motionManager.updateParam(live2DModel);             //设置了动作后,更新模型的参数

        //表情的动作是一直保持的
        if (Input.GetKeyDown(KeyCode.E))
        {
            expressionManager.startMotion(expressions[expressionIndex]);

            print("expression index: " + expressionIndex + "\n");

            expressionIndex++;
            if (expressionIndex >= expressionFiles.Length)
            {
                expressionIndex = 0;
            }
        }
        expressionManager.updateParam(live2DModel);

        Vector3 mousePos = Input.mousePosition;         //获得鼠标的坐标

        //更新模型参数,使模型随着鼠标运动
        l2DTargetPoint.Set(mousePos.x / Screen.width * 2 - 1, mousePos.y / Screen.height * 2 - 1);      //将鼠标坐标缩放到[-1, 1],然后存储到l2DTargetPoint中
        l2DTargetPoint.update();

        //从l2DTargetPoint中取出坐标,用于更新模型的参数
        live2DModel.setParamFloat("PARAM_ANGLE_X", l2DTargetPoint.getX() * 30);
        live2DModel.setParamFloat("PARAM_ANGLE_Y", l2DTargetPoint.getY() * 30);
        live2DModel.setParamFloat("PARAM_BODY_ANGLE_X", l2DTargetPoint.getX() * 10);
        //live2DModel.setParamFloat("PARAM_BODY_ANGLE_Y", l2DTargetPoint.getY() * 10);
        live2DModel.setParamFloat("PARAM_EYE_BALL_X", l2DTargetPoint.getX());
        live2DModel.setParamFloat("PARAM_EYE_BALL_Y", l2DTargetPoint.getY());

        //更新模型的参数,放在Updat()函数后面
        live2DModel.update();
    }