How to Make Joystick's Direction Relative to A Rotating Object?

I have a camera that rotates whilst following the player’s movement and I can get the joystick’s direction in world space:

// rightX and rightY being the input for the x and y axis of right stick respectively
Vector3 stickDirection = new Vector3 (rightX, 0, rightY);

However as per how the image is illustrated below, my goal is to have the stick direction to be relative to the camera’s.

So when I press UP for example, which in this case is rightY == 1, the direction will always be pointing to the transform.forward of the camera, no matter what position it is and where it looks at.

I have tried something like

Vector3 stickDirection = this.transform.InverseTransformDirection(rightX, 0, rightY);

But it’s not behaving the way I wanted, need the direction for my targeting system. Would love to hear some solutions from you pros out there.

Assuming the camera is not tilted, you can simply multiply the ‘stickDirection’ by the rotation of the camera before applying the direction to any movement. You are using InverseTransformDirection() so I assume you are using Transform.Translate() to move the object? Anyway, you can do it like this:

Vector3 stickDirection = new Vector3(rightX, 0.0f, rightY);
stickDirection = Camera.main.transform.rotation * stickDirection;
stickDirection = transform.InverseTransformDirection(stickDirection);

It might be easier to stick with world direction and not do the InverseTransformDirection. You can specify Space.World in the Translate() function to move in world rather than local coordinates.