Camera translation not consistent

I have a script attached to my camera that allows me to translate when holding left mouse and rotate when holding left mouse and shift. Translate works as intended until a rotation happens. If the camera is rotated 180 degrees along y, the translation is reversed. How can I make the translation work the same regardless of the cameras rotation. Thanks for any help.

Heres a snippet

        if (isMouseMoving == true)
        {
            if(Input.GetKey("left shift"))
            {
                transform.eulerAngles += rotateSpeed * new Vector3(0, Input.GetAxis("Mouse X"), 0);
            }
            else
            {
                transform.position += dragSpeed * new Vector3(-Input.GetAxis("Mouse X"), 0, -Input.GetAxis("Mouse Y"));
                dragOrigin = Input.mousePosition;
            }

1 Answer

1

I may be misunderstanding what you want, but If you want to move relative to the current rotation, you can use transform.TransformDirection(), to convert a vector from local to global coordinates:

transform.position += dragSpeed * transform.TransformDirection(new Vector3(-Input.GetAxis("Mouse X"), 0, -Input.GetAxis("Mouse Y")));