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!