Instead of raycasting, you can convert the GridMove.js to use the character.controller.move function.
(I apologize for raising a necro topic, but this is exactly what I am doing and I hope it helps people in the future.)
private var moveDirection : Vector3 = Vector3.zero;
function Start ()
{
//needed to move character controller so we collide with colliders in scene
var controller : CharacterController = GetComponent(CharacterController) as CharacterController;
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 == Orientation2.Horizontal?
Vector3(Mathf.Round(myTransform.position.x), 0.4, Mathf.Round(myTransform.position.z)) +
Vector3(System.Math.Sign(input.x)*gridSize, 0.4, 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);
//Above line uses translation to move object, below uses character controller
// based on input vector2 directions.
if ( input == (Vector2(1.0, 0.0))) //right
{
moveDirection = (Vector3(1.0, 0.0, 0.0));
}
if ( input == (Vector2(-1.0, 0.0))) //left
{
moveDirection = (Vector3(-1.0, 0.0, 0.0));
}
if ( input == (Vector2(0.0, 1.0))) //up
{
moveDirection = (Vector3(0.0, 0.0, 1.0));
}
if ( input == (Vector2(0.0, -1.0))) //down
{
moveDirection = (Vector3(0.0, 0.0, -1.0));
}
controller.Move (moveDirection);
yield;
}
tx = t - 1.0;
GetAxes();
}
}
So you just comment out the “myTransform.position = Vector3.Lerp(startPosition, endPosition, t);” line,
and put cases for dependency on which input comes in. This way all of your methods for the player’s input
can simply tell GetAxes(); to handle input based on the vector2.
Meaning you can have both input from the defined input inside the project settings, AND GUI buttons that
handle the input changes as well.
input = Vector2(1.0, 0.0); changing the two vectors depending on which direction the player enters, this one is ‘right’ for example, is all you need to do by sending it to GetAxes. I’ve tested this out as I am editing GridMove.js myself.
The changes in the above Start function allow for the character to collide. You will have to play with the math a bit
to get the distance you want.
Eric’s GridMove.js is a very nice addition to the community.