【问题标题】:Unity colision trigger to start an animation [closed]Unity碰撞触发器开始动画[关闭]
【发布时间】:2020-05-21 19:23:58
【问题描述】:

我需要编写一个 C# 脚本来制作改变角色的动画。情况:用户(VR用户)靠近并进入一个npc的碰撞区域,该npc处于坐着的动画循环中。一旦用户进入 npc 的碰撞区域,npc 将动画更改为“坐立”,并从那里一系列动画到达另一个地方。如何编辑此脚本以确保在触发碰撞区域后发生动画更改。这是我从 youtube turorial 找到的代码,一旦他进入触发区域,它就会显示用户名。如果需要更多信息,请告诉我。

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

public class sittingtrigger : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        void onTriggerEnter(Collider other)
        { Debug.Log(other.name); }
    }
}

【问题讨论】:

    标签: c# visual-studio unity3d animation


    【解决方案1】:

    触发方法是在Update之外调用的方法。

    public class sittingtrigger : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
    
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
        void onTriggerEnter(Collider other)
        { 
            Debug.Log(other.name); 
            //here you have to set the variables of your animator to trigger the animation
        }
    }
    

    记得将 trigger-zone-collider 设置为“Is Trigger”!

    【讨论】:

    • 我使用什么样的变量? (对不起,如果这些问题看起来很愚蠢,我对此非常陌生)。我将 mixamo 用于动画,所以我将这些文件导入到统一中
    • 试试这个:youtube.com/watch?v=JS4k_lwmZHk 如果不适合你告诉我,我会更新这个答案好吗? ^^
    【解决方案2】:

    像 Lotal 所说的那样将 onTriggerEnter 放在外部更新。

    还有一个动画师。

    public Animator animator;
    

    并将检查器中的动画师设置为统一编辑器中脚本上的插槽。 从动画状态机中的“任何状态”设置转换。然后你可以做类似的事情

    animator.SetTrigger("StandAnimation");
    

    在任何状态和新动画之间的状态机中,您单击链接并添加条件,在参数中您应该创建了一个触发器“StandAnimation”,然后在条件中设置它。这只是一个示例,您可以使用任何您想要的名称,您也可以使用 int、bool、float 代替 trigger。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      • 2013-09-04
      相关资源
      最近更新 更多