Move object to mouse position in 3D world. Dynamic Camera!
Hi. 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.
Answer by tiagofilipduarte · Jul 22, 2020 at 09:06 AM
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
}
else
{
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;
}
return Vector3.zero;
}
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613003651im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third person look around camera, jagged results 0 Answers
3d camera not working as I want it to 0 Answers
Maximum Camera Angle Rotation 0 Answers
Camera rotation 2 Answers