【问题标题】:How can I make the npc character to rotate slowly facing the player while changing animator transitions?如何让 npc 角色在更改动画转换时面向玩家缓慢旋转?
【发布时间】:2018-06-19 14:25:00
【问题描述】:

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

但是要查看它是如何工作的,以及它是否像我想要的那样工作得很好,我必须关闭 FPSController Camera(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 exit the door > 士兵慢慢地从地面切换到瞄准模式,士兵慢慢地旋转面对 FPSController。

我的问题是如何在 FPSController 退出门时启动 Cinemachine 过场动画,以及如何让 Soldier(npc) 面向 FPSController 缓慢旋转。

我在脚本中的更新中尝试过的效果不佳。 它使 npc 移动并且没有像我想要的那样在过场动画中缓慢旋转。

也许对于旋转部分,我应该使用 Cinemachine 而不是在脚本中这样做?也许我也应该在这里使用 Timeline 作为旋转部分?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我需要的工作解决方案:

    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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多