How to get a dynamic z value for block placing in a specific area?
Hello people, I have sat with this problem for 6 hours now and I can't seem to find the solution for this. Hopefully you can help me out.
I'm working on a project where the player can build objects in a 3d environment. I have a designated building area where the player can only place blocks. This building area is a boxcollider with a 3 dimensional value. First we used a raycast to detect where to place blocks, but now we are trying to replace the system by using your mouse position as the target position for the blocks.
Now the problem is of course when I use ScreenToWorldPoint, the z value remains 0. I can hardcode this to a certain value of course, but I want this value to dynamically change according to the mouse position on the screen(As we see in many building games today).
so the higher up your mouse goes on the screen, the further away your block will be placed in the area where you can build things. And the problem is we need to do the things a raycast does, without using the raycast.
Hopefully I have made my problem clear and someone can help me. Let me know if things are unclear and I will gladly explain!
You dont want screentoworldpoint, you want screenpointtoray and cast a ray from the mouse position.
https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Check out this question I recently answered.
http://answers.unity3d.com/questions/1255792/move-object-to-mouse-position.html#answer-1255831
You mention not using a raycast. Any reason why?
Youre trying to convert 2d coordinates (screen coordinates) to 3d space (world space). If you are placing it on the ground, I guess the Y coordinate of your Vector3 could be the ground height and the X/Z values would be the mouse position on screen.
block.position = new Vector3(buildArea.position.x + (mouse.x * buildAreaWidth), groundHeight, buildArea.position.z + (mouse.y * buildAreaHeight));
That is assu$$anonymous$$g your pivot point for the "build area" is at the lower left corner of the buildable area. If it is anywhere else, you would have to offset. Say the pivot is in the center and you want to build around that..
block.position = new Vector3((buildArea.position.x - (buildAreaWidth/2f)) + (mouse.x * buildAreaWidth), groundHeight, (buildArea.position.z - (buildAreaHeight/2f)) + (mouse.y * buildAreaHeight));
If I use the code you first described, I get insanely high numbers. I forgot to mention this, but the building area is a gameobject that stands on a terrain. the terrain is pretty big, it's 500 by 500 units.
$$anonymous$$aybe I can use a raycast to get the depth dynamically. I will try this next and I will let you know!
Edit: @b1gry4n I tried your solution, but the example block i'm using to see where the block will be placed doesn't enter the boxcollider, because the raycast hits the border of the collider. Any more suggestions maybe?
Edit2: @b1gry4n I almost have it now! the only problem is that my example block only starts moving deeper into the building area after I left the center of the screen. Is there a way to circumvent this?
Your answer

Follow this Question
Related Questions
Making GameObject move in the direction of my mouse (3d) 0 Answers
Help me! Create a game object falling from the top scene at click mouse point! T________T 0 Answers
How can I make the game object follow the mouse? 1 Answer
Mouse follow help 0 Answers
how to have a gameobject move with the mouse cursor 3D whenclicked on? 0 Answers