Move object to mouse position in 3D world. Dynamic Camera!

I am trying to make an object follow the mouse in a 3d scene where the camera position and rotation might change. I already had it working before when the camera was static using ScreenToWorldPoint and the mouse delta movement on some situations. This object will always be restricted to two axis ( x and y or x and z).

Is there any way that when given the mouse position i can get the world position while respecting the camera position and rotation.

So, I found a solution and I’m going to share it with you to help anyone that might have the same issue…

The solution is to use ray tracing to find the exact position of the mouse relative to the world and the object

public Vector3 GetPiecePlaneMousePos(Vector3 targetPos, bool isOnWall = false)
    Plane plane;
    if (!isOnWall)
        plane = new Plane(Vector3.up, targetPos); // x & z
        plane = new Plane(Vector3.forward, targetPos); // x & y

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition + distanceToTarget);
    float dist;
    if (plane.Raycast(ray, out dist))
        Vector3 finalPos = rulerWorldToLocal.MultiplyPoint3x4(ray.GetPoint(dist));
        return finalPos;

The distanceToTarget will calculate the offset between the mouse click and the object pivot so that the movement doesn’t force the object to center with the mouse position…

The rulerWorldToLocal is going to convert my world coordinates to local coordinates according to my ruler, that’s the parent object of the pieces i am moving…

After that move the object to vector return by the function and it should work!