# How to get sprite following mouse position when camera is rotated 30 degree on X axys on UNITY3D?

I’m trying to get a sprite following my mouse position with my camera rotated at 30 on x axys, this works fine if camera have a rotation of 0,0,0 but not on 30,0,0, how I have to calculate this? I have tryed substracting to x position with no success, here is my code:

this is attached on the object I want to follow the mouse

``````private void FixedUpdate()
{
Vector3 pos = cam.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(pos.x, pos.y, transform.position.z);
}
``````

You are calculating the world point corresponding to the mouse position, but then throwing away one of the coordinates and substituting a different value instead.

When the camera is looking in the +Z direction, the Z coordinate is just how far away the object is. But when the camera is facing in some other direction, that’s no longer true, so changing that coordinate means the object will no longer be in line with the mouse.

You could just set `transform.position = pos` (using all 3 coordinates from ScreenToWorldPoint), but then you’ll no longer be controlling how far away the object is, so you just get whatever distance ScreenToWorldPoint returns.

The example in the documentation for ScreenToWorldPoint shows using a starting point that takes the mouse X and Y but different Z, so that’s probably a good way to control the distance. However, you won’t simply be able to use transform.position.z as the distance, because that will be changing as the object moves around; you’ll need to somehow refactor the code so that the desired distance of the object is represented explicitly.

Thanks for the answer, as I could see getting the Z of mouse set’s the object z aswell so might get hidden behind assets, also my camera is ortographic, why I couldn’t just ignore the Z? its confusing

Because your camera is rotated. After rotation world Z axis is not equal to the camera Z axis anymore. That’s expected behaviour. Camera Z will be it’s transform.forward vector.

The same reason you can’t ignore X or Y. Changing any coordinate means the object is in a different position. If you calculate a special position that makes the object look like it’s under the mouse cursor, but then you move it somewhere else, then it probably won’t look like it’s under the mouse cursor anymore.

The only direction you can move the object while still making it look like it’s under the mouse cursor is directly towards or away from the camera. When the camera was looking exactly along the Z axis, that means you could move it along the Z axis*. But when the camera is facing some other direction, now you can only move it along whatever direction the camera is facing (which is probably a combination of multiple coordinates).

• Actually, this might not be 100% true if you are using perspective projection and the object is not directly in front of the camera. In that case, you’d have to move the object along the line between it and the camera, which might not be the same as the direction the camera is looking. But it was probably pretty close before.

If you want the object to be under the mouse, then the only thing you’re allowed to pick is how far away it is–all other parts of its position are dictated by the requirement that it follow the mouse.

There are two main strategies you might use for picking how far away to put it:

1. Pick a specific distance from the camera (use that distance as the Z coordinate of the point you pass as an argument to the function ScreenToWorldPoint).

2. Define a surface that it can move along (for example, the floor of your world, or the plane of your canvas). In this case, you should use a raycast from the camera through the mouse and see where it hits that surface, and that will tell you where to put your object.