gpt4 book ai didi

c# - 通过脚本 C# 在特定点移动 unity3d 中的门

转载 作者:行者123 更新时间:2023-11-30 15:19:36 27 4
gpt4 key购买 nike

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

关于c# - 通过脚本 C# 在特定点移动 unity3d 中的门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41951832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com