需要帮助使用动画而不是脚本旋转来打开门

问题描述 投票:0回答:3

我的门是用脚本打开的,所以我想用动画打开它。如果有人知道如何解决这个问题,那将是非常有帮助的。这是控制开门部分的脚本部分。

我已经尝试过编写脚本,但我似乎找不到适合Unity的脚本方法。 (我正在使用最新版本的Unity)。

 if (open)
        {
            var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0f, -90.0f, 0f), Time.deltaTime * 200);
            transform.rotation = newRot;

            Ray ray = new Ray(transform.position, transform.forward);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, interactDistance))
            {
                if (hit.collider.CompareTag("Door"))
                {
                hit.collider.transform.parent.GetComponent<doorOpen>().ChangeDoorState();
                }
            }
        }
        else
        {
            var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0f, 0f, 0f), Time.deltaTime * 200);
            transform.rotation = newRot;
        }

我正用一把钥匙打开我的门,我想用门打开动画。任何帮助都会很棒。

c# unity3d animation 3d communityengine
3个回答
1
投票

通常,您将创建和动画,以更改游戏对象的所需变换。 (在你的情况下,Y旋转。)

然后在动画制作窗口中,设置一个可以调用该动画的触发器,然后在正确的RayCast命中它时调用它。您可以创建条件逻辑以反向播放,也可以创建关闭动画。

    animator.setTrigger("Open");

这样做的好处是,您可以轻松控制门的开启速度。


0
投票
  1. 用门创建一个动画片段并保存
  2. 使用门游戏对象添加动画组件
  3. 然后把你创建的动画放在那里

0
投票

。hit.collider.transform.parent.GetComponent()ChangeDoorState();应该已经触发了动画。不是这个。

private Animator _animator;

void Start()
{
    _animator = GetComponent<Animator>();
}

void OnTriggerEnter()
{
    if (other.tag == "Player")
    _animator.SetBool("open", true);
}

“'open'”是你指定给门的动画。确保在检查员中添加'animator'。

© www.soinside.com 2019 - 2024. All rights reserved.