Move and rotate 2d object using virtual joystick

Here is my code for moving a 2D top-down object using the built in virtual joystick:

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

Without the rotation code, the movement was perfect, after adding the rotation code the movement got all messed up, if the direction is down, it moves up. But the rotation is exactly how it should.

Try something like this:

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

        // Some arbitrary smallish value, so that it remains in the
        // same direction if it hardly moves
        if (moveVec.sqrMagnitude > .1f)
        {
            transform.right = moveVec;
        }

        transform.Translate(moveVec * Time.deltaTime * speed,[URL='https://docs.unity3d.com/ScriptReference/Space.World.html']Space.World[/URL]);
);
    }
1 Like