- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
abhishakverma · Mar 10, 2014 at 04:26 PM ·
c#javascriptjava
How to move the object at the center of the grid cell?
Look at the attach image and the script,i want to player to move and stop only at the center of next grid cell. please let me know if anyone know it.
[1]: /storage/temp/23377-gameview.jpg
pragma strict
// Move object using accelerometer
var speed = 10;
function Start () {
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x =/*Input.acceleration.x*5;*/ Input.GetAxis("Horizontal");//Input.acceleration.x*5;//Input.GetAxis("Horizontal");//-Input.acceleration.y;
dir.z =/*Input.acceleration.y*2;*/Input.GetAxis("Vertical");//Input.acceleration.y*5;//Input.GetAxis("Vertical"); //Input.acceleration.x;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// stop to cross movement
if(Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) {
dir.z = 0;
}
else {
dir.x = 0;
}
// Move object
transform.Translate (dir * speed);
}
gameview.jpg
(67.7 kB)
Comment