Modifing the 'GridMove' script

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]

The key would be modifying the GetAxes function, so that for example “7” would make input.x = -1.0 and input.y = 1.0, “9” would make input.x = 1.0 and input.y = 1.0, etc.

–Eric

As i said, do not understand this script, can you show me how to do it for one of the keys so i know how. Thanks you just for replying, if you don’t reply again thanks anyway.

You don’t have to understand the rest of the script, just modify that one function. Give it a try.

–Eric

Thanks Eric, i found out how to do it. To anyone else looking for how to do it in the future, here is the modified GetAxes function:

function GetAxes () {
    input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    if (allowDiagonals) {
		if(Input.GetKeyDown("[7]")) {
			input.y = 1.0;
			input.x = -1.0;
		}		
		if(Input.GetKeyDown("[9]")) {
			input.y = 1.0;
			input.x = 1.0;
		}
		if(Input.GetKeyDown("[1]")) {
			input.y = -1.0;
			input.x = -1.0;
		}		
		if(Input.GetKeyDown("[3]")) {
			input.y = -1.0;
			input.x = 1.0;
		}
			return;
		}
    if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
        input.y = 0.0;
    else
        input.x = 0.0;
}

Hey there can someone explain to me just briefly the System.Math.Sign/B part of the code.
I couldn’t find it in the reference guide.
thanks for any feedback

System.Math.Sign. The problem with Mathf.Sign is that it returns 1 when the input is 0, which I would consider incorrect, since 0 isn’t positive.

Actually it doesn’t, which is why I don’t use Mathf.Sign.

–Eric

Oh, is that the problem with Mathf.Sign? I’ve read somewhere that Mathf.Sign is supposedly not as accurate as System.Math.Sign and could not figure out for the life of me what that was about (since from my point of view, a method that returns either +1 or -1 can’t possibly get it wrong). Hmm… the coders prolly forgot about 0 and just put “return 1” under a blanket else branch.