• 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 a_boy_with_hair · Feb 28, 2020 at 11:13 AM · gridgrid based gamesnapsnappinggrids

Grid snapping

Hello there, I am trying to make a building system and I have created a grid and can place a cube down and the other cubes can snap to the very first one, but they only snap to the first one, not to all the other ones. here is a picture for reference alt text

As I said previously they can only snap to the first cube. And I don't know what I am missing here so I can make it snap to all the cubes.


I am going to post my 3 script files here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WallCollider : MonoBehaviour
 {
     Foundation foundationScript;
     Vector3 sizeOfFoundation;
 
     // Start is called before the first frame update
     void Start()
     {
         foundationScript = transform.parent.parent.GetComponent<Foundation>();
         //cube must have similiar length sides
         sizeOfFoundation = transform.parent.parent.GetComponent<Collider>().bounds.size;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private void OnTriggerEnter(Collider other)
     {
         //create snapping ability
         if (BuildManager.isBuilding && other.tag == "Wall" && !other.GetComponent<Foundation>().isSnapped)
         {
             //so we can call foundation within this function
             Foundation foundation = other.GetComponent<Foundation>();
 
             //get mouse position
             foundation.isSnapped = true;
             foundation.mousePosX = Input.GetAxis("Mouse X");
             foundation.mousePosY = Input.GetAxis("Mouse Y");
 
 
 
 
 
             float sizeX = sizeOfFoundation.x;
             float sizeZ = sizeOfFoundation.z;
 
             switch(this.transform.tag)
             {
                 case "WestCollider":
                     other.transform.position = new Vector3(transform.parent.parent.position.x - sizeX, 0, transform.parent.position.z);
                     break;
                 case "EastCollider":
                     other.transform.position = new Vector3(transform.parent.parent.position.x + sizeX, 0, transform.parent.position.z);
                     break;
                 case "NorthCollider":
                     other.transform.position = new Vector3(transform.parent.parent.position.x, 0, transform.parent.position.z + sizeZ);
                     break;
                 case "SouthCollider":
                     other.transform.position = new Vector3(transform.parent.parent.position.x, 0, transform.parent.position.z - sizeZ);
                     break;
 
             }
         }
     }
 }
 



 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Foundation : MonoBehaviour
 {
     public bool isPlaced;
     public bool isSnapped;
 
     public float mousePosX;
     public float mousePosY;
 
     // Update is called once per frame
     void Update()
     {
         //if wall is not yet placed
         //we need it to follow our mouse using raycast
         if (!isPlaced && !isSnapped)
         {
             //tell buildmanager that we are building
             BuildManager.isBuilding = true;
 
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray,out hit))
             {
                 this.transform.position = new Vector3(hit.point.x, 0, hit.point.z);
             }
         }
 
         //placing wall down using left mouse button
         if (Input.GetMouseButtonDown(0))
         {
             isPlaced = true;
             BuildManager.isBuilding = false;
         }
 
 
         //release snapping ability for wall
         //mathf.abs is to check if old and new positions are different
         if (isSnapped && !isPlaced && Mathf.Abs(mousePosX - Input.GetAxis("Mouse X")) > 0.2f || Mathf.Abs(mousePosY - Input.GetAxis("Mouse Y")) > 0.2f)
         {
             isSnapped = false;
         }
     }
 }
 


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BuildManager : MonoBehaviour
 {
     //default bool is false
     public static bool isBuilding;
     public GameObject wallPreFab;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Alpha1) && !isBuilding)
         {
             //set true to prevent clones.
             isBuilding = true;
             //build a cube at coordinated 0,0,0 when key "1" is pressed
             Instantiate(wallPreFab, Vector3.zero, wallPreFab.transform.rotation);
         }
 
 
     }
 }
 


Anything helps really. Thank you for your time.

snap.png (28.7 kB)
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

123 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

Related Questions

Snap to grid edge midpoints? 1 Answer

Snap to grid not working for me 1 Answer

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

How to drag gameobject (only x y) snapped to a grid? 2 Answers

Gizmo won't stop snapping to grid. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges