0
\$\begingroup\$

I am try make simple flying camera. Without character. But than I press forward camera moving to another direction.

    void Update()
{
    horizontalInput = Input.GetAxisRaw("Horizontal");
    verticalInput   = Input.GetAxisRaw("Vertical");

    var mouseX = Input.GetAxis("Mouse X");
    var mouseY = Input.GetAxis("Mouse Y");

    var trm = Camera.main.transform;

    if (horizontalInput != 0 || verticalInput != 0)
    {
        trm.position += moveSpeed * new Vector3(horizontalInput, 0, verticalInput);
    }

    var rotateValue = new Vector3(mouseY * 2, mouseX * -2, 0);

    trm.eulerAngles -= rotateValue;

    //trm.rotation = Quaternion.Euler(rotateValue);
}

enter image description here

Than I press 'W' camera moving to left... I understand that it is necessary to coordinate the motion vectors and camera rotation. How best to do it?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Use Transform.transformDirection().

if (horizontalInput != 0 || verticalInput != 0)
{
    Vector3 vec = moveSpeed * new Vector3(horizontalInput, 0, verticalInput);
    trm.position += trm.transformDirection(vec);
}

It basically converts a vector direction from the transform's local space into world space. So that transform's rotation is going to affect that vector.

\$\endgroup\$
1
  • \$\begingroup\$ Can you answer to another question. How to rotate the camera around the axis of look direction. \$\endgroup\$
    – nim
    Commented Jul 23, 2019 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .