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 /
avatar image
0
Question by Rugbug_Redfern · Apr 11, 2018 at 04:43 PM · instantiatemathgridgrid based game

Grid-based building system not working

Hey.

I'm making a survival game and I am trying to build a building system similar to Rust.

This is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SnapToGrid : MonoBehaviour {
 
     public GameObject buildingPrefab;
 
     public float gridDistance = 2f;
 
     float gridSize;
 
     Vector3 gridPosition;
     Vector3 worldPosition;
 
     void Start() {
         worldPosition = transform.position;
 
         gridSize = 1f / gridDistance;
     }
 
     void Update() {
 
         gridSize = 1f / gridDistance;
 
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f));
 
         RaycastHit hit;
 
         Physics.Raycast(ray, out hit);
 
         worldPosition = hit.point;
 
         //Vector3 distanceVec = hit.point - Camera.main.transform.position;
         //worldPosition -= distanceVec.normalized * (distanceVec.magnitude - 1f);
 
         gridPosition = new Vector3(
         Mathf.Round(worldPosition.x * gridSize) / gridSize,
         Mathf.Round(worldPosition.y * gridSize) / gridSize,
         Mathf.Round(worldPosition.z * gridSize) / gridSize);
 
         transform.position = gridPosition;
 
         if(Input.GetMouseButtonDown(0)) {
             Instantiate(buildingPrefab, gridPosition, Quaternion.identity);
         }
     }
 }

There is a preview where the raycast from the camera comes in contact with a collider, and whenever you click it will instantiate the building in the place where the preview is.

alt text

HOWEVER... The problem I currently have is whever you try to place a building on the side of another building, it clips through and goes inside that building.

alt text

This makes it impossible to place one building on the side of another, and it also makes it so you can stack multiple buildings in the exact same position. These are both things I do not want.

Do you guys know of any way to make the preview on the side of the placed building instead of inside it? Thanks, Rugbug

screenshot-80.png (269.1 kB)
screenshot-81.png (256.4 kB)
Comment
Add comment · Show 1
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 Cherno · Apr 11, 2018 at 06:50 PM 0
Share

Use Physics.Boxcast to check if any existing buildings are in the space. If yes, compare the construction space center with the existing building's center to find the side of the existing building that is nearest, and then move the space accordingly to it sits next to the existing building.

1 Reply

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

Answer by EnigmaticCrow · Apr 11, 2018 at 06:48 PM

I did a similar thing a while ago and had the same problem. My solution was to add the (normalized) normal of the surface you've hit to the point of the raycasthit before rounding it to the gridposition. You can get the normal of the mesh at a specific point by using RayCastHit.triangleIndex and taking the average of the three normals.

Comment
Add comment · Show 1 · 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
avatar image Rugbug_Redfern · Apr 11, 2018 at 07:09 PM 0
Share

Thanks a ton!

 Vector3 newPos = hit.point + hit.normal.normalized;

is what I ended up doing

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

101 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

Related Questions

mouse position percentage problem 0 Answers

Keeping track of which areas remain unexplored 1 Answer

How to handle Grid Boxes properly? 1 Answer

The 'correct' way to deal with animations in a grid-based game? 1 Answer

Block puzzle (1010) help 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