How do I rotate a vector3 that I'm using for movement?

I’m struggling trying to adapt Cinemachine (and other tools) to my project and right now I’m trying to get my movement vector to match the camera direction.

So I’m looking around with cinemachine and I want to apply force to player that rotates with the camera y axis.

If im pressing forward and have a vector of (0,0,1) and the camera is looking right, I want to rotate that vector so its now (1,0,0).

Make sense?

I’m sure its gonna need angleaxis, but everything I’ve tried is not working.

here’s the current code, if that helps:

    void ApplyMovment()
    {
        Vector3 appliedMovement = new Vector3(movementHeading.x, 0, movementHeading.y);
        if (isGrounded) appliedMovement *= Time.fixedDeltaTime * walkSpeed;
        if (!isGrounded) appliedMovement *= Time.fixedDeltaTime * airSpeed;
        appliedMovement = Quaternion.Euler(0, camera.rotation.y, 0) * appliedMovement;
        //appliedMovement = Quaternion.AngleAxis(camera.rotation.y, Vector3.up) * appliedMovement;
        rb.AddForce(appliedMovement);
    }

EDIT:

This was the solution if anyone is wondering

    void ApplyMovment()
    {
        Vector3 appliedMovement = new Vector3(movementHeading.x, 0, movementHeading.y);
        if (isGrounded) appliedMovement *= Time.fixedDeltaTime * walkSpeed;
        if (!isGrounded) appliedMovement *= Time.fixedDeltaTime * airSpeed;
        appliedMovement = Quaternion.Euler(0, myCamera.eulerAngles.y, 0) * appliedMovement;
        //appliedMovement = Quaternion.AngleAxis(camera.rotation.y, Vector3.up) * appliedMovement;
        cameraRotation = myCamera.eulerAngles.y;
        rb.AddForce(appliedMovement);
    }

You can multiply a quaternion by a directional vector to get the rotated vector. It’s the * operator: Unity - Scripting API: Quaternion.operator *

Basically Vector3 rotatedDirection = rotation * originalDirection.

You just need to generate the correct rotation first.

1 Like

Yup, and use euler not rotation.

Thanks again, btw.

I mean looking at your problem again, why don’t you just use the Transform.forward of the camera itself?

Because I’m dumb.