Grid snapping like MaK?

Hey! Could anybody provide me with a script or a way to snap cubes to a grid like in this video: http://www.youtube.com/watch?v=7sXnwa9bt6g Thanks in advance! :slight_smile:

This is a awesome looking little game. It reminds me of my little game I am working on little Genius.

That game truly is awesome and I don’t know why but it kinda reminds me of games made in Unity. Really looking forward to it! :slight_smile: What I want to imitate is the grid snapping mechanics for the cubes. Any ideas? :slight_smile:

You could just have triggers, and hinges I guess. If, box in hand, it is an active trigger. If not, it is not a trigger. If box in hand touches box on ground, if hold box btn active, do nothing. If hold box btn released, set hinge joint.

Here is a simple script that allows you to move an object using the mouse.
It also snaps the object to a grid.

#pragma strict

function Update () {

   var mousePos = Input.mousePosition;
   var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, 5));
   
   var xPos = Mathf.Round (wantedPos.x);
   var yPos = Mathf.Round (wantedPos.y);
   
   transform.position.x = xPos;
   transform.position.y = yPos;

}

Hopefully this will be a good starting point.

Thanks a lot! I’ll see what I can do with this script. :slight_smile: If anybody can offer a complete solution, I would appreciate it, but thanks anyway :slight_smile:

I’ve got a problem. I converted the script I was given to C# but I get errors. Script:

 using UnityEngine;
using System.Collections;

public class CubeController : MonoBehaviour 
{
	void Update()
	{
		Vector3 mousePos = Input.mousePosition;
		Vector3 wantedPos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 5));
		
		float xPos = Mathf.Round(wantedPos.x);
		float yPos = Mathf.Round(wantedPos.y);
		
		transform.position.x = xPos;
		transform.position.y = yPos;
	}
}

And the errors I get are:

Cannot modify a value type return value of ‘UnityEngine.Transform.position’. Consider storing the value in a temporary variable.

You cannot modify directly transform.position (latest 2 lines of your code) single coords in C# (same for rotation), so you need to change it completely:

transform.position = new Vector3(xPos, yPos, transform.position.z);

Thanks that worked, but still, how do identify if the cursor is on another cube’s side? (That’s the only place I want cubes to spawn, not just anywhere…)

Have you checked out my Grid Framework extension? It works by providing a new Grid component for objects, what you see in MaK could be done by adding such a component to each block. Then perform a linecast from the player, when it hits a cube in the scene while you are carrying another cube the snapping will occur relative to the Grid component of the first cube. That would let you combine two cubes. How do you add cubes to an existing cluster? Basically the same way since grids inside one cluser overlap it doesn’t matter which cube’s grid you use as long as they are in the same cluster. Grids are components that inherit from MonoBehaviour, so they will follow their object’s position and rotation.

Here is a basic code mockup:

//find the cube in the world
if(player.isCarrying){
     if(Physics.Linecast(player.position, player.position + endDirection, hit)){ //find the cube we want to be oriented to
          worldCube = hit.rigidbody.GameObject;
     }
     if(!worldCube) // we aren't close to any cube in the world, don't do anything
          return;

     theGrid = worldGube.GetComponent.<GFGrid>(); //this is the grid we want to use
     var worldPoint = hit.point + hit.normal * 0.5 * worldCube.edgeLength // find a world point inside the ox where we want our cube to place
     
     carriedCube.transform.position = worldPoint; // put the cube approcimately at the right position
     theGrid.AlignTransform(carriedCube.transform); //align the cube properly

     carriedCube.transform.parent = worldCube.transform;
}

Linecasting has this thing I called hit, it’s of type RaycastHit and contains all sorts of information. The challenge is in finding the side of the sube we want our cube to attach to, once we have it we shift the hit point a little to the outside and then GridFramework gives us the proper position. I wrote hit.normal * 0.5 * worldCube.edgeLength in my code, you could really use any length instead, the importand part is that it faces away from the side we hit. Just make sure the length doesn’t to in the next grid box.