在更改动画制作者过渡时,如何使NPC角色面向播放器缓慢旋转?

时间:2018-06-19 14:25:00

标签: c# unity3d

我在士兵动画师中有一个Grounded过渡,默认情况下是从该过渡开始的。然后,我做了3秒钟后,它将在从“接地”到“ Rifle_Aiming_Idle”的两个过渡之间缓慢变化。

但是要查看其工作方式以及是否如我所愿地正常工作,我必须关闭FPSController摄像头(FPSCamera禁用为灰色)。因此,在运行游戏时,活动摄像机是主摄像机和CM vcam1(Cinemachine虚拟摄像机)。

但是我想用这个过场动画做更多。 我希望首先,过场动画将仅在FPSController玩家离开门时开始:

此脚本已附加到FPSController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class SpaceshipCutscene : MonoBehaviour
{
    public Transform player;
    public Transform npc;
    public float cutsceneDistance = 5f;
    public float speed;

    private bool moveNpc = false;

    // Use this for initialization
    void Start()
    {

    }

    private void Update()
    {
        if (moveNpc)
        {
            float travel = Mathf.Abs(speed) * Time.deltaTime;

            Vector3 direction = (player.position - npc.position).normalized;
            Quaternion lookrotation = Quaternion.LookRotation(direction);
            npc.rotation = Quaternion.Slerp(npc.rotation, lookrotation, Time.deltaTime * 5);
            Vector3 position = npc.position;
            position = Vector3.MoveTowards(position, player.position, travel);
            npc.position = position;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.name == "Horizontal_Doors_Kit")
            moveNpc = true;
    }
}

我希望当玩家FPSController离开门时启动过场动画,并且在播放/执行过场动画时,我还希望同时使带有动画师的士兵朝FPSController缓慢旋转。

FPSController退出大门>士兵从地面缓慢变为瞄准模式,士兵朝FPSController缓慢旋转。

我的问题是当FPSController离开门时如何启动电影放映机以及如何使Soldier(npc)面向FPSController缓慢旋转。

我在脚本的“更新”中尝试执行的操作不正常。 就像我想要的那样,它使NPC可以在过场动画中移动而不缓慢旋转。

Anim

也许对于旋转部分,我应该使用Cinemachine,而不要在脚本中使用它?也许我也应该在此处将时间轴用于旋转部分?

1 个答案:

答案 0 :(得分:0)

适合我需要的解决方案:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class SpaceshipCutscene : MonoBehaviour
{
    public Transform player;
    public Transform npc;
    public Camera FPSCamera;
    public Camera mainCamera;
    public Animator anim;
    public float rotationSpeed = 3f;

    private bool moveNpc = false;

    // Use this for initialization
    void Start()
    {

    }

    private void Update()
    {
        if (moveNpc)
        {
          Vector3 dir = player.position - npc.position;
        dir.y = 0; // keep the direction strictly horizontal
        Quaternion rot = Quaternion.LookRotation(dir);
        // slerp to the desired rotation over time
        npc.rotation = Quaternion.Slerp(npc.rotation, rot, rotationSpeed * Time.deltaTime);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.name == "Horizontal_Doors_Kit")
        {
            FPSCamera.enabled = false;
            mainCamera.enabled = true;
            moveNpc = true;
            anim.SetTrigger("SoldierAimingTrigger");
        }
    }
}