Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by bakos · Dec 28, 2013 at 05:32 PM · transparencyplacementblock

Preview selected gameobject at hit.point

Hello everyone!!

I am not going to lie, but I do not have the code for it what I need as of this moment, but I feel it is easier than I am making it.

What I would like help on:

A script which will place a transparent version of my selected block at the hit.normal of where my block is going to be placed.

So I can see where my block will be placed before I even place it.

If anyone out there can help me with this, PLEASE feel free to do so :P I have looked EVERYWHERE and I have no luck finding anything to fix my issue.

Thank you for reading, Sincerely,

Bakos133

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Dec 29, 2013 at 07:01 AM

There is a lot of detail missing from your question. The sample code I'm providing assumes blocks of 1x1x1.

  • Create a cube in the scene visible from the camera (GameObject > Create Other > Cube).

  • Add the tag "Cube" to the tag manager and Tag this cube with the new tag

  • Create a second cube, and name it "Preview"

  • Create a new material. Select the Transparent/Diffuse shader for this material. Select the color and change the alpha to 75 (out of 255).

  • Attach the new material to the 'Preview' cube.

  • Turn off or delete the collider on the 'Preview' cube.

  • Attach the following script to the 'Preview' cube.

  • Select the 'Preview' cube in the hierarchy. Drag and drop the original cube (the one tagged 'Cube') onto the blockPrefab variable of this script in the Inspector.

  • Add a directional light

  • Hit run.


    pragma strict

    var blockPrefab : GameObject;

    function Update() { var hit : RaycastHit; var ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, hit) && hit.collider.tag == "Cube") { var pos = hit.transform.position; pos += hit.normal; renderer.enabled = true; transform.position = pos; if (Input.GetMouseButtonDown(0)) { Instantiate(blockPrefab, pos, Quaternion.identity); } } else { renderer.enabled = false; } }

Note I added a modified version of MouseOrbit code to he camera so I could test this scrip by adding cubes from all sides. I added this line just inside Update() on the MouseOrbit script:

 if (!Input.GetKey(KeyCode.LeftShift)) return;

That way I could hold the shift key to rotate but but easily click on the block when the left shift key was not held down.

Note if you are going to be building larger structures, you don't want to build them out of individual game objects. Look into voxels:

http://unitycoder.com/blog/2012/10/18/voxel-resources-engines-for-unity/

Comment
Add comment · Share
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

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

18 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

Related Questions

Minecraft like building? 2 Answers

Lego brick placement 0 Answers

increasing scale based on mouse position 0 Answers

Transparent Texture 2 Answers

shader transparency 1 Answer


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