微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过脚本C#在unity3d中的特定位置移动门

sample pic of my desire solution

我的游戏中有两个门.最初它们是关闭的.之后,它们将打开,并会在特定点停止(图片作为示例附上).到目前为止,我已经编写了一个脚本,该脚本可以连续旋转门.我想阻止他们,例如以45度角,需要种种建议.

using UnityEngine;
using System.Collections;

public class rotate : MonoBehavIoUr 
{
    public string rotate_along = "y";
    public float speed = 10.0f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () 
    {
                if (rotate_along == "y") {
                        this.transform.Rotate (0, speed, 0);
                } else if (rotate_along == "x") {
                        this.transform.Rotate (speed * Time.deltaTime, 0, 0);
                } else if (rotate_along == "z") {
                        this.transform.Rotate (0, 0, speed * Time.deltaTime);
                } else {
                        print ( "please! check your cordinate for rotating for "+gameObject.name );
                }
    }
}

解决方法:

我建议您为此使用Lerp或Slerp:

void Update() {
        transform.rotation = Quaternion.Lerp(from.rotation, to.rotation, Time.time * speed);
}

[Source]

这将使您的门自然顺畅地移动,就像在现实世界中一样,并使您避免在向量空间中编码恐怖. Unity社区提供了很多有关四元数如何工作的示例.在这里,您可以找到区别的简要说明:

http://answers.unity3d.com/questions/389713/detaliled-explanation-about-given-vector3slerp-exa.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐