Move around Z axis

Hey guys, i am making a simple game to release for free on my friend’s new gaming website. It is a simple point and click game. It’s almost finished (it’s very easy to make) and i just need to fix a problem that i don’t know how to solve. In the game, there is a part where you need to hit some nails with a hammer. The hammer is behind the camera and has an animation to hit the nails. My little big problem is, the nails are in different locations of the screen, and i don’t know how to move my hammer around the Z axis. I wanted to make a script that would detect where the player hit on the screen, and then move it to the more acurate location in the Z axis, like in the example:

But i have no experience in using the mouse in scripts, and let’s say i am not the best at using transform.position I know how to change the position accordingly, i just don’t know how to make the hammer be moved to X position and how to detect where the player clicked. Thanks for the help!

This is what you’re looking for.


Vector3 clickPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);

float zPosition = clickpoint.z;

hammer.transform.position = new Vector3(hammer.transform.position.x, hammer.transform.position.y, zPosition)