Does this code compile? The big issues is that ScreenToWorldPoint() takes a Z parameter that is the distance into the scene to place the object. I’ll assume you don’t want the Z value to change for your crosshair object. This assumes you are looking down the Z axis. This can be written more compactly (less steps), but for the sake of understanding:
Vector3 v3Pos = Input.mousePosition;
// Set 'Z' to the distance in front of the camera;
v3Pos.z = Mathf.Abs(CrossHair.transform.position.z - Camera.main.transform.position.z);
v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
v3Pos.y = CrossHair.transform.position.y;
CrossHair.transform.position = v3Pos;
Thank You, but it didnt work maybe because my Game TopDown not FPS? so i dont need to move CrossHair Up And Down , it should be at the player height i guess we shouldn't use Camera in this situation some times the code show this error transform.position assign attempt for 'CrossHair(Clone)' is not valid. Input position is { -Infinity, 2.000000, Infinity }. UnityEngine.Transform:set_position(Vector3) Crosshair:Update() (at Assets/Scripts/MultiPlayer/Crosshair.cs:35)
Thank You, but it didnt work maybe because my Game TopDown not FPS? so i dont need to move CrossHair Up And Down , it should be at the player height i guess we shouldn't use Camera in this situation some times the code show this error transform.position assign attempt for 'CrossHair(Clone)' is not valid. Input position is { -Infinity, 2.000000, Infinity }. UnityEngine.Transform:set_position(Vector3) Crosshair:Update() (at Assets/Scripts/MultiPlayer/Crosshair.cs:35)
– D3m0nE