Alright, this script (found here: http://www.unifycommunity.com/wiki/index.php?title=GridMove) is a really awesome script that makes movement on a grid really simple. I love the script but i do not understand it completely so my endeavors to modify it so you can press a new button (i am thinking about using the numpad so 7 and 9 for forward diagonals and 1 and 3 for reverse diagonals and 4,2,6,8 for regular movement) instead of having to press both left AND right at the SAME time, which is tricky and not the best way to do it. As i said i do NOT know how the script works so i can’t modify it to do this, so i am asking you guys for help. Here is the script:
var walkSpeed : float = 1.0;
var runSpeed : float = 2.0;
var gridSize : int = 1;
enum Orientation {Horizontal, Vertical}
var gridOrientation = Orientation.Horizontal;
var allowDiagonals = false;
var correctDiagonalSpeed = true;
private var input = Vector2.zero;
function Start () {
var myTransform = transform;
var startPosition : Vector3;
var endPosition : Vector3;
var t : float;
var tx : float;
var moveSpeed = walkSpeed;
while (true) {
while (input == Vector2.zero) {
GetAxes();
tx = 0.0;
yield;
}
startPosition = myTransform.position;
endPosition = gridOrientation == Orientation.Horizontal?
Vector3(Mathf.Round(myTransform.position.x), 0.0, Mathf.Round(myTransform.position.z)) +
Vector3(System.Math.Sign(input.x)*gridSize, 0.0, System.Math.Sign(input.y)*gridSize)
:
Vector3(Mathf.Round(myTransform.position.x), Mathf.Round(myTransform.position.y), 0.0) +
Vector3(System.Math.Sign(input.x)*gridSize, System.Math.Sign(input.y)*gridSize, 0.0);
t = tx;
while (t < 1.0) {
moveSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
t += Time.deltaTime * (moveSpeed/gridSize) * (correctDiagonalSpeed input.x != 0.0 input.y != 0.0? .7071 : 1.0);
myTransform.position = Vector3.Lerp(startPosition, endPosition, t);
yield;
}
tx = t - 1.0; // Used to prevent slight visual hiccups on "grid lines" due to Time.deltaTime variance
GetAxes();
}
}
function GetAxes () {
input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
if (allowDiagonals)
return;
if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
input.y = 0.0;
else
input.x = 0.0;
}
Thanks in advance. Also if you do don’t understand what i mean ask me. Or if you do NOT want to do this FOR me, then please try to break the script down so i can understand it. Thanks again. [/url]