使用虚拟操纵杆移动和旋转2d对象

时间:2016-12-06 02:38:08

标签: unity3d rotation transform joystick

以下是使用内置虚拟操纵杆移动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);  
    }

没有旋转代码,运动是完美的,在添加旋转代码后,运动全部搞砸了,如果方向向下,它会向上移动。 但旋转正是它应该如何。

1 个答案:

答案 0 :(得分:0)

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

相关问题