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!
Answer by conman79 · Nov 29, 2016 at 11:50 PM
This is what you're looking for.
https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
Example:
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)
It doesn't register the position of the mouse vector stay at 0,0,0
Your answer
Follow this Question
Related Questions
Variable value does not change 0 Answers
Change object using script 0 Answers
Button OnClick function? 1 Answer
How do you change your z axis in a movement script. 1 Answer
Game Object jumps to z-axis 0 Answers