how do I find velocity relative to where my player is looking?

I found this code off the internet that does this, however, i don’t understand what’s going on, for example, why does the cosine of the difference angle get multiplied by the magnitude? here it is:

public Vector2 FindVelRelativeToLook() {
    float lookAngle = orientation.transform.eulerAngles.y;
    float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;

    float u = Mathf.DeltaAngle(lookAngle, moveAngle);
    float v = 90 - u;

    float magnitue = rb.velocity.magnitude;
    float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
    float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
    
    return new Vector2(xMag, yMag);
}

the cosine of given angle is a fraction that represents the proportion of the length of the adjacent side to the the length of the hypotenuse of a right triangle. So, to make a usable, real world value out of it, you need to multiply it by another value - it scales the cosine value.

Say you want to put a point in 2D space that is 30 degrees up from the x axis. (rotated on x counter clockwise by 30 degrees) and at a distance of 2 meters from the center of the world. To plot this point, given that information, you can calculate the x coordinate and y coordinates using the sine and cosine trig functions.

2 x cos( 30 degrees ) gives you the x coordinate. In this example, to get y - the length of the opposite side, you can use the sine trig function. ( or, you could use cos( 60 degees ) because you’d be using the adjacent side of the right angle compliment of 30 )

2 x sine( 30 degrees ) gives you the y coordinate. // cos(60 degrees) gives you the same value

In sum, trig functions give you proportions based on 1. You need to scale them into real world distances. In your code, I’d need some time to wrap my head around what it is doing.

You should check out this video, i am sure after watching this your doubt will be cleared :
this is video by Sebastian Lague : here is video : Introduction to Game Development (E22: trigonometry) - YouTube
This Code is all about trigonometry