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 AlrexXboy · Jun 15, 2021 at 05:37 PM · scripting probleminstantiateraycast

How to place blocks so they stick to a surface in any orientation? And stick to a grid?

I'm making a game where one of the mechanics involves placing prefab building blocks, and I want them to stick to a grid and stick to other already placed blocks to make building easier. I've tried different approaches and I'm starting to get confused.

My main issue comes with placing the blocks at the right distance from a surface; when you place them on perfectly flat ground it's easy enough to figure it out, but I want to allow the player to place blocks on walls without changing the block's pitch/roll. This means the block will always be upright, and will only rotate on the Y-axis.

Right now here's what I've implemented: when you point towards a surface you can build on, a "ghost block" appears that shows you how the block will look once placed, and also checks whether or not the block is intersecting anything. Left-click spawns the block on this position, right-click applies a 45-degree rotation to the block, and the scroll wheel allows you to cycle through different blocks, which are chosen from an array in my code.

This is the method where, after a raycast to check if the player is pointing towards a surface you can build on that is close enough, the position and rotation of the block are calculated, and it also checks if the block can be placed at that position. This code works properly when placing on the floor. When placing blocks on walls, however, since I'm only using the vertical scale of the block to calculate the distance it should spawn from the surface, it doesn't work.

 bool CheckPlaceable(RaycastHit hitInfo)
     {
         Collider other = hitInfo.collider;
 
         ghosts[selectedBlock].SetActive(true); //activate the ghost
 
         normal = hitInfo.normal; //normal of the face the raycast has hit
         eulerRotation = new Vector3(0, angle); //rotation that the player has assigned to the block
 
         position = (hitInfo.point + (blocks[selectedBlock].transform.localScale.y / 2 * normal)); //places the block on top of the point
 
         ghosts[selectedBlock].transform.position = position;
         ghosts[selectedBlock].transform.localRotation =  Quaternion.Euler(eulerRotation); //rotate the block
 
         if (!ghostCollision.Intersecting) //if the block isn't going to be placed where it intersects with 
                                                                //anything (the ghost block serves to check this)
         {
             ghostRenderer.material = ghostPlaceable;
             return true;
         }
         else
         {
             ghostRenderer.material = ghostNotPlaceable;
             return false;
         }
     }

And my second question, how can I implement a grid system in a way that doesn't interfere with this? I already tried simply rounding the block's position to the unit but it was a bit janky since it didn't take sticking to surfaces into account.

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

Answer by AlrexXboy · Jun 16, 2021 at 02:57 PM

I think I figured out a solution for the first question, by using the bounding box of the block and the normal and point of the raycast. This works for surfaces lined up with world coordinates, but I can't get any further with this approach I think. Still, close enough.

 Renderer blockMesh = currentBlock.GetComponent<Renderer>(); //Get the renderer's bounding box
         Vector3 blockBounds = blockMesh.bounds.size; //take the rotation into account from the start by using bounding box
         Debug.Log(blockBounds);
 
         Vector3 moveVector = new Vector3(normal.x * blockBounds.x, normal.y * blockBounds.y, normal.z * blockBounds.z) / 2; 
         Debug.Log("move by " + moveVector);
 
         position = hitInfo.point + moveVector;

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

265 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 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 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

Bullethole doesn't instantiate 1 Answer

Offseting insantiated object 1 Answer

Raycast bullet impact and instantiating particles 3 Answers

Help with moving instantiated object's transform 1 Answer

My Raycast acts like it's hitting something when it isn't 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