【问题标题】:Translating a Unity 2D Code into 3D将 Unity 2D 代码转换为 3D
【发布时间】:2017-06-17 07:11:51
【问题描述】:

我得到了一个用于 2D 摆动斧头的小脚本。就像这里的这个:

Dark Souls Swinging Axe

我的问题是,我不知道如何将 2D 代码转换为 3D。我的对象附有刚体和铰链接头(用于摆动物理)。

Rigidbody.angularVelocity

需要获取 Vector3 而不是浮点值。

我的代码:

   Rigidbody rigid;
    float rangeToPush = 0.3f;
    float velocity = 120;

    private void Start()
    {
        rigid = GetComponent<Rigidbody>();
        rigid.angularVelocity = Vector3.down * velocity;
    }

    private void Update()
    {
        if (transform.rotation.z > 0 &&
            transform.rotation.z < -rangeToPush &&
            rigid.angularVelocity.magnitude > 0 &&
            rigid.angularVelocity.magnitude < velocity)
        {
            rigid.angularVelocity = Vector3.down * velocity;
        }

        else if (transform.rotation.z < 0 &&
            transform.rotation.z > rangeToPush &&
            rigid.angularVelocity.magnitude < 0 &&
            rigid.angularVelocity.magnitude > -120)
        {
            rigid.angularVelocity = Vector3.down * -velocity;
        }
    }

缺少的东西是

的价值
data.Rigid.angularVelocity = ?Vector3?

而不是我的浮点值,显然我的最后两个 if 语句是错误的,我有

if( /* the other statements .. && */ data.Rigid.angularVelocity > 0 && data.Rigid.angularVelocity < data.RigidVelocity)

我需要将其转换为 3D。

非常感谢您的帮助!

编辑:我尝试使用

data.Rigid.angularVelocity = Vector3.down * data.RigidVelocity;

对于我的 if 语句

Rigid.angularVelocity.magnitude

但这会在有限的时间内摆动,而不是无穷无尽的。所以也许我必须更改数据脚本中的值,或者我必须使用其他东西而不是幅度......我不知道

【问题讨论】:

  • 我只会用 Animation 为其制作动画,或者用脚本和 AnimationCurve 移动它......
  • 是的,我只是想为它制作动画。但我觉得脚本更好,这就是我问的原因:)
  • 选择平面 x/y 或 y/z 并以这种方式实现,忽略左侧的轴。或者保留 2d 的东西,不要显示它并将变换传递给 3d 对象(可能效率低下)

标签: c# math unity3d physics


【解决方案1】:

我终于明白了。我删除了 HingeJoint 并使用此代码使其无休止地摆动:

    float speed = 0.4f; // Speed
    float amplitude = 70; // How far can it swing?

    private void Update()
    {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Sin(Time.timeSinceLevelLoad * Mathf.PI * speed) * amplitude)); // Swing
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2013-03-28
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多