Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Bl0odyMe5s · Oct 15, 2016 at 12:36 PM · gameobjectmousepositiondepthblock

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image b1gry4n · Oct 15, 2016 at 06:16 PM 0
Share

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));


avatar image Bl0odyMe5s · Oct 15, 2016 at 07:00 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges