Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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.

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 object to grid with offset? 1 Answer

RTS building snap to grid 2 Answers

Snap All Axes Hotkey? 0 Answers

Snapping to Grids 0 Answers

Create Grid and Snap Rigidbodies to Grid 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