Rigidbody velocity to local space

Yes, I have tried transform.TransformDirection. It does make my character move forward in whatever direction it is facing, however, it also makes it moves forward when I press S, and when I press A or D, it moves… backward. transform.TransformVector is even weirder. So please help me make my velocity convert to local space. Here is my code:

private void FixedUpdate()
    {
        Vector3 myVelocity = new Vector3((HorizontalInput * Speed), 0f, (ForwardInput * Speed));
        if (VerticalInput == 1)
        {
            playerRb.velocity = new Vector3(myVelocity.x, VerticalInput * VerticalPower, myVelocity.z);
        } else
        {
            playerRb.velocity = transform.TransformDirection(new Vector3(myVelocity.x, playerRb.velocity.y, myVelocity.z));
        }
        
    }

Hello, I think you asked the same question a few hours ago and already got an answer.

Replace everything you have in the Fixed Update with this code and it should work: (Assuming all your input variables are controlled by input axes)

private void FixedUpdate()
{
    Vector3 myVelocity;

    myVelocity.x = HorizontalInput * Speed;
    myVelocity.y = VerticalInput * VerticalPower;
    myVelocity.z = ForwardInput * Speed;

    playerRb.velocity = Quaternion.Euler(playerRb.transform.eulerAngles) * myVelocity;
}

It’s not the best performing code and not the best chosen variable names, but it should work.

Ok, I found out that my character controller method for looking in the same direction as the camera (credits to Brackeys) was turning my character in the direction I wanted him to go and then moving him in said direction. This was because in the Brackeys tutorial the character turns the way they move, which is not what I want. Thanks to everyone who tried to help me. I’ll still be using @anszwa’s code though,(Thanks!) it’s a lot cleaner.