# Help with turn based Movement in a grid

Hello everyone, I’m new and I need some advice on how to implement the grid system movement using the mouse, a bit like in the series of Heroes of Might and Magic. I created the battlefield using this script that generates tiles:
Grid.js

``````public var tilePrefab: GameObject;
public var numberOfTiles: int = 10;
public var tilesPerRow: int = 4;
public var distanceBetweenTiles: float = 1.0;

function Start()
{
CreateTiles();
}

function CreateTiles()
{
var xOffset: float = 0.0;
var zOffset: float = 0.0;

for(var tilesCreated: int = 0; tilesCreated < numberOfTiles; tilesCreated += 1)
{
xOffset += distanceBetweenTiles;

if(tilesCreated % tilesPerRow == 0)
{
zOffset += distanceBetweenTiles;
xOffset = 0;
}

Instantiate(tilePrefab, Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z + zOffset), transform.rotation);
}
}
``````

Then I created a script to give to an object, called Target, which basically allows you to move it into the “grid” using the mouse.
Target.cs

``````using UnityEngine;
using System.Collections;

public class Target : MonoBehaviour
{

public bool selected = false;
public float floorOffset = 1;
public float speed = 5;
public float stopDistanceOffset = 0.5f;

private Vector3 moveToDest = Vector3.zero;
private bool selectedByClick = false;

private void Update ()
{

//		if (renderer.isVisible && Input.GetMouseButton(0))
//		{
//			if (!selectedByClick)
//			{
//				Vector3 camPos = Camera.mainCamera.WorldToScreenPoint(transform.position);
//				//camPos.y = Mouse.InvertMouseY(camPos.y);
//				//selected = Mouse.selection.Contains(camPos);
//			}

//			if (selected)
renderer.material.color = Color.red;
//			else
renderer.material.color = Color.white;
//		}

if (selected && Input.GetMouseButtonDown(1))
{
Vector3 destination = Mouse.GetDestination();

if (destination != Vector3.zero)
{
//gameObject.GetComponent<NavMeshAgent>().SetDestination(destination);

moveToDest = destination;
moveToDest.y += floorOffset;
}

}
if (selected && Input.GetMouseButtonDown(0))
{
selected = false;

}

UpdateMove();
}

private void UpdateMove()
{
if (moveToDest != Vector3.zero && transform.position != moveToDest)
{
Vector3 direction = (moveToDest - transform.position).normalized;
direction.y = 0;
transform.rigidbody.velocity = direction * speed;

if (Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
moveToDest = Vector3.zero;
}
else
transform.rigidbody.velocity = Vector3.zero;

}

private void OnMouseDown()
{
selectedByClick = true;
selected = true;
}

private void OnMouseUp()
{
if (selectedByClick)
selected = true;

selectedByClick = false;
}

}
``````

The only thing is that the movement appears to be “free”, not connected to the grid.

How can I do so that it is connected to the grid? So that the object moves per block ??

Thanks and sorry for my bad English
I hope I was clear enough

Hello, sorry for asking in such an old thread, but did u manage to make it work ? Thanks

Hello! No, I gave up and bought the TBTK asset by SongGameDev! ;D