Mouse move object according to grid

Hello people!

I’m making a 2D Turn based strategy - actually I’m recreating NINJA BURAI DENSETSU for Genesis -, and I’m cracking my head on one issue.

In the original game the cursor moves within a grid, just moving it up, down, left or right, and i did that well, as you can see below:

BUT since I want to improve the gameplay, I want the player to use the cursor with the mouse, but not moving freely: the cursor would move grid-snapped, just like the video above, got it?

Here’s the code I attach to the cursor (I commented my mouse attempts):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class userCursorController : MonoBehaviour {


    public float horizontalInput;
    public float verticalInput;

    //Mouse Variables
    private Vector3 mousePosition;
    public float moveSpeed = 0.1f;

    //Cursor move Variables
    private float moveRate = 0.15f;
    private float nextMove = 0.0f;

    //Use this for initialization
    void Start() {
        transform.Translate(1.0f, 1.0f, 0.0f);
        // Cursor.visible = false;
    }

    // Update is called once per frame
    void Update() {
        cursorMove();
       // mouseMovement();
       



    }

    void mouseMovement()
    {
      /*  Vector3 posicaoMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        posicaoMouse.z = 0;

        Debug.Log("aaa " + posicaoMouse.x);
        */
        /* ( > gameObject.transform.localScale){ 

        gameObject.transform.position = posicaoMouse;
         } */
        }
    void cursorMove()
    {

        verticalInput = Input.GetAxis("Vertical");
        horizontalInput = Input.GetAxis("Horizontal");

        if(horizontalInput == 1.0f && nextMove < Time.time)
        {

           transform.Translate(1.0f, 0.0f, 0.0f);
            nextMove = Time.time + moveRate;
        }
        else if(horizontalInput == -1.0f && nextMove < Time.time)
        {
           transform.Translate(-1.0f, 0.0f, 0.0f);
            nextMove = Time.time + moveRate;
        }

        if (verticalInput == 1.0f && nextMove < Time.time)
        {
            transform.Translate(0.0f, 1.0f, 0.0f);
            nextMove = Time.time + moveRate;
        }
        else if (verticalInput == -1.0f && nextMove < Time.time)
        {
            transform.Translate(0.0f, -1.0f, 0.0f);
            nextMove = Time.time + moveRate;
        }



        
        

    }
}

Thank you so much, guys!

You can achieve that effect much easier - just use the “TileMap” class.

Add a new tilemap to your hierarchy (GameObject menu ->2D Object → Tilemap), set it up to match the right size for your game. In your code first get your mouse position in world coordinates.

You can pass it to your tilemap to get the coordinates of the tile on your tilemap (as Vector3Int) using TileMap.WorldToCell method.

Then you can use those coordinates to get the world coordinates of the middle of that tile using Tilemap.GetCellCenterWorld. Use those to position your cursor.

Hmmm, didn’t know that class.
I’m gonna test it!

Thank you so much!

use this to make your

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class crumov : MonoBehaviour {
 
     public float horizontalInput;
     public float verticalInput;
 
     //Mouse Variables
     public Vector3 mousePosition;
     public float moveSpeed = 0.1f;
     public Vector2 SquaresWeHaveMoved = Vector2.zero;
 
     //Cursor move Variables
     public float moveSize = 1f;
 
     //Use this for initialization
     void Start() {
         //transform.Translate(1.0f, 1.0f, 0.0f * Time.deltaTime);
         // Cursor.visible = false;
     }
 
     // Update is called once per frame
     void Update() {
         cursorMove();
        // mouseMovement();
 
     }

    void define()
     {
     // we are making a new var named SquaresWeHaveMoved then we are using that to get the movment done we are also using another var named moveSize. 
     //this is basicily the size of eacth square is in the grid
     // we are adding one to the Squares we have moved IF WE DID get more than 1 of what we hade it is hard to explain to me =(
     // we are basicily adding the x and the y of our mouse
     // after all that is put together IN CODE then this can work out =)
     // fill free to use this on your games ;)  
     }
 

         }
     void cursorMove()
     {
         //Debug.Log(SquaresWeHaveMoved.x, SquaresWeHaveMoved.y); 
         mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         verticalInput = mousePosition.y;
         horizontalInput = mousePosition.x;
 
          if (verticalInput > (SquaresWeHaveMoved.y + 1)* moveSize)
          {
           transform.position=new Vector3(SquaresWeHaveMoved.x * moveSize, (SquaresWeHaveMoved.y +1) * moveSize,0).normzlized;  
           SquaresWeHaveMoved.y++;
          }

          if (verticalInput < (SquaresWeHaveMoved.y - 1)* moveSize)
          {
           transform.position=new Vector3(SquaresWeHaveMoved.x * moveSize, (SquaresWeHaveMoved.y -1) * moveSize ,0).normzlized;  
           SquaresWeHaveMoved.y--;
          }

          if (horizontalInput > (SquaresWeHaveMoved.x + 1)* moveSize)
          {
           transform.position=new Vector3((SquaresWeHaveMoved.x +1) * moveSize, SquaresWeHaveMoved.y * moveSize ,0).normzlized;  
           SquaresWeHaveMoved.x++;
          }

          if (horizontalInput < (SquaresWeHaveMoved.x - 1)* moveSize)
          {
           transform.position=new Vector3((SquaresWeHaveMoved.x +1) * moveSize , SquaresWeHaveMoved.y * moveSize ,0).normzlized;  
           SquaresWeHaveMoved.x--;
          }
         
      }
 
     
 }