- Home /
 
 
               Question by 
               kurukshetran · Nov 01, 2015 at 07:18 AM · 
                3dgridcubegridmove  
              
 
              Moving the cube in a grid cell by cell
I am learning unity by trying different stuffs. I created a grid of 6*6 cells. Each cell prefab is a cube. And i instantiated a player cube at 0,0 position in the grid and i wrote simple cube movement script. What i am trying to do is move cube cell by cell movement and not a continuous movement. Here is the script
 using UnityEngine;
 using System.Collections;
 public class PlayerController : MonoBehaviour 
 {
 private Vector3 pos;
 private bool moving = false;
 
 // Use this for initialization
 void Start ()
 {
     pos = transform.position;
 }
 
 // Update is called once per frame
 void Update ()
 {
     CheckInput();
     
     if(moving)
     {
         transform.position = pos;
         moving = false;
     }
 }
 
 void CheckInput()
 {
     if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) 
     {
         pos += Vector3.right;
         moving = true;
     }
     else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) 
     {
         pos -= Vector3.right;
         moving = true;
     }
     else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) 
     {
         pos += Vector3.forward;
         moving = true;
     }
     else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) 
     {
         pos -= Vector3.forward;
         moving = true;
     }
 }
 }
 
               Here is the web build of visual.
https://dl.dropboxusercontent.com/u/102812893/CUBE/CUBE.html
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Having trouble with grid movement 1 Answer
How to instantiate a prefab at mouse pos 1 Answer
Making 3D Objects Stick On Other Objects Like A Grid? 2 Answers
Faults with Grid-Based movement... 2 Answers