【问题标题】:Move and rotate 2d object using virtual joystick使用虚拟操纵杆移动和旋转 2d 对象
【发布时间】:2016-12-06 02:38:08
【问题描述】:

这是我使用内置虚拟操纵杆移动 2D 自上而下对象的代码:

void Update () 
    {
        Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), 
            CrossPlatformInputManager.GetAxis("Vertical"));


        Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), 
            CrossPlatformInputManager.GetAxis("Vertical"), 4000);

        transform.rotation = Quaternion.LookRotation(lookVec, Vector3.back);
        transform.Translate(moveVec * Time.deltaTime * speed);  
    }

没有旋转代码,运动是完美的,加上旋转代码后运动就乱了,如果方向向下,它就会向上移动。 但轮换正是应有的方式。

【问题讨论】:

    标签: unity3d rotation transform joystick


    【解决方案1】:

    只需将 transform.Translate(moveVec * Time.deltaTime * speed); 行更改为 transform.Translate(moveVec * Time.deltaTime * speed, Space.World); :默认情况下,相对于变换自身空间 (Space.Self) 进行平移。

    【讨论】:

    • 这很好用,我不想烦人,但如果你能解释一下,什么时候用“Space.World”什么时候不用,我将不胜感激。跨度>
    • @Abdou023 好吧,这取决于情况...在这种情况下,Transform.Translate documentation 声明将在转换的本地空间中进行翻译:这意味着翻译将受对象方向的影响。您可以在 ParticleSystem 组件中找到相同类型的行为:假设您有一个火焰粒子生成器在您的场景中移动,使用局部空间时火焰将保持笔直,而使用世界空间时火焰将弯曲。希望这会有所帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多