Snapping Target to Mouse Position
I've looked through some other answers and I've only found something close to what I needed. I'm looking for help with a script that allows a target to snap to the mouse position throughout the entire game.
This is what the target looks like when not in play mode. I have attached the following script (Credit: @tanoshimi ) in an attempt to have the target go to the current mouse position, but when I enter play mode, the target seems to be rapidly alternating between two positions. It is alternating between 5.05 and -2.7 in the z-axis, and alternating at a range of units in the y-axis based on the current position of the camera in the y-axis:
{
void Update ()
{
Vector3 temp = Input.mousePosition;
temp.z = 7.75f;
this.transform.position = Camera.main.ScreenToWorldPoint(temp);
}
}
Any help with this would be greatly appreciated. In case this piece of information helps, the camera is currently following the cube along the x and y axes and the cube is clamped in the z-axis.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612075114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse Cursor is flashing 0 Answers
Targeting Script Errors 0 Answers
Centering camera between my player and a selected object. 0 Answers
Third Person Camera Challenge (C#) 0 Answers
Mouse turns invisible when you return to the main menu or help menu 0 Answers