简体   繁体   English

将Unity 2D代码转换为3D

[英]Translating a Unity 2D Code into 3D

I got a small Script for a swinging Axe in 2D. 我有一个用于2D旋转斧头的小脚本。 Just like for example this one here: 就像这里的一个例子:

Dark Souls Swinging Axe 黑魂摇摆斧

My problem is, that I do not know how to translate the 2D code into 3D. 我的问题是,我不知道如何将2D代码转换为3D。 My object got a Rigidbody and a Hinge Joint (for swinging Physics) attached. 我的对象附有一个刚体和一个铰链接头(用于摆动物理)。 The

Rigidbody.angularVelocity

needs to get a Vector3 instead of a float value. 需要获取一个Vector3而不是一个float值。

My Code: 我的代码:

   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;
        }
    }

The things missing are the value for 缺少的东西是有价值的

data.Rigid.angularVelocity = ?Vector3?

instead of my float value and obviously my two last if-statements are wrong, i have 而不是我的浮点值,显然我的最后两个if语句是错误的,我有

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

and I need to convert it into 3D. 我需要将其转换为3D。

Thanks a lot for help! 非常感谢您的帮助!

Edit: I tried to use 编辑:我试图使用

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

and for my if-statements 对于我的if陈述

Rigid.angularVelocity.magnitude

but this is gonna swing for a limited time, not endless. 但这将在有限的时间内摇摆,而不是无尽的。 So maybe I have to change a value in the Data Script or I have to use something else instead of magnitude ... I do not know 所以也许我必须在数据脚本中更改一个值,或者我必须使用其他内容代替幅度...我不知道

i finally got it. 我终于明白了。 I removed the HingeJoint and used this code to make it swing endless: 我删除了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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM