- Home /
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
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
Your answer
Follow this Question
Related Questions
How to drag gameobject (only x y) snapped to a grid? 2 Answers
Gizmo won't stop snapping to grid. 1 Answer
Grid and or Tile System 0 Answers
Snap object to grid with offset? 1 Answer