- Home /
Character Control - Grid like movement
Hello, I am having a problem getting my character to move the way I would like. I recently purchased the book (Game Development with Unity) and have been following along with an example they have on how they set the character up. This was a great start... however, I am wanting my character to move more like a grid system setup as typically seen in top-down old-school RPG or strategy games. I am new to Java script and any help would be greatly appreciated.
What I have so far...
//Widget_Controller: Handles Widget's movement and player input
//Widget's Movement Variables------------------------------------------------------------------
//These can be changed in the Inspector
var rollSpeed = 6.0;
var gravity = 20.0;
var rotateSpeed = 4.0;
//private, helper variables-----------------------------------------------------------------------
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var moveHorz = 0.0;
private var normalHeight = 2.0;
private var rotateDirection = Vector3.zero;
var isControllable : boolean = true;
//cache controller so we only have to find it once----------------------------------------------
var controller : CharacterController ;
controller = GetComponent(CharacterController);
//var widgetStatus : Widget_Status;
//widgetStatus = GetComponent(Widget_Status);
//Move the controller during the fixed frame updates------------
function FixedUpdate() {
//check to make sure the character is controllable and not dead
if(!isControllable)
Input.ResetInputAxes();
else{
if (grounded) {
// Since we're touching something solid, like the ground, allow movement
//Calculate movement directly from Input Axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if ( moveDirection != Vector3.zero )
transform.rotation = Quaternion.LookRotation( moveDirection ); // This line will allow him to rotate the direction he is moving
moveDirection *= rollSpeed;
}
}
// Apply gravity to end Jump, enable falling, and make sure he's touching the ground
moveDirection.y -= gravity * Time.deltaTime;
// Move and rotate the controller
var flags = controller.Move(moveDirection * Time.deltaTime);
controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
grounded = ((flags & CollisionFlags.CollidedBelow) != 0 );
}
//---------------------------------------------------------------------------------------------------------------------------
function IsMoving(){
return moveDirection.magnitude > 0.5;
}
function IsGrounded(){
return grounded;
}
////Make the script easy to find
@script AddComponentMenu("Player/Widget'sController")
I'm currently working on a similar game but I've yet to tackle this issue. However, if you are going to use a grid based system like in Final Fantasy I-VI/Pokemon, bear in $$anonymous$$d that those games have your character "snap" to the grid, meaning that there will be no jumping (making IsGrounded obsolete), no rotation other than the 4 main directions.. long story short, what I mean is that using a character controller may not be the most suitable thing to do for such a project.
Answer by Eric5h5 · Nov 19, 2011 at 12:23 PM
Hi Eric5h5, Thank you for your response. I have looked into the Grid$$anonymous$$ove script and it works great. However, I need it to do one additional thing... I am not sure how I can get it to rotate my character so that it is facing the direction it is moving. I have tried adding something like this... transform.rotation = Quaternion.LookRotation( moveDirection ); but have add zero luck in getting it to rotate my character. Do you know I can get this to work?
There have been lots of questions (and answers) about making rotation match direction of movement; you should be able to find one with a search.
Answer by pixelburst · Nov 19, 2011 at 09:28 PM
Wanted to post a quick update... I found a solution to my character rotation issue using the GridMove script. I needed to add the following line of code...
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")));