- Home /
Character Controller falls through ground when not using Input.GetAxis()
I have a small problem with controlling a character with a Character Controller when not using Input.GetAxis()
I prefere to use Input.GetKey() for all (or most) of my input, but the problem is when i don't use Input.GetAxis() the character falls to the ground but then just sort of disapeares after about half a second.
I don't understand why it would work with one but not the other.
var playerSpeed : float = 6.0; var playerGravity : float = 20.0; var playerJumpSpeed : float = 8.0;
private var playerMove : Vector3 = Vector3.zero;
function Update() { var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
if(Input.GetKey(KeyCode.T))
playerMove = Vector3(0,0,1);
if(Input.GetKey(KeyCode.G))
playerMove = Vector3(0,0,-1);
if(Input.GetKey(KeyCode.F))
playerMove = Vector3(-1,0,0);
if(Input.GetKey(KeyCode.H))
playerMove = Vector3(1,0,0);
//Works with this line...but not the above code
//playerMove = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
playerMove = transform.TransformDirection(playerMove);
playerMove *= playerSpeed;
if(Input.GetKey(KeyCode.J)){
playerMove.y = playerJumpSpeed;
}
}
playerMove.y -= playerGravity * Time.deltaTime;
controller.Move(playerMove * Time.deltaTime);
}
It just seems strange that one will work, but the other will give me some very strange results :/
anyone else had problems like this or is it that you have to use Input.GetAxis()?
Answer by benni05 · May 03, 2011 at 08:11 PM
Input.GetAxis returns a value between -1 and +1. Not just -1 or +1, please check this out through
Debug.Log(Input.GetAxis("Horizontal"));
in one of your Update methods and have a look at these values. So probably your game scale doesn't fit to those hardcoded -1/+1 values you get when using the t,g,f,h keys.
Ben
Answer by Shogun · May 03, 2011 at 09:21 PM
Reading your answer i managed to fingure the problem out. With Input.GetAxis() you are always getting value being passed 'playerMove'...
With my code above i was only passing a value (1 or -1) if a key was being pressed. I made some changes to the code above...
[snip...]
if(Input.GetKey(KeyCode.UpArrow)){
playerMove = Vector3(0,0,1);
}
else if(Input.GetKey(KeyCode.DownArrow)){
playerMove = Vector3(0,0,-1);
}
else if(Input.GetKey(KeyCode.LeftArrow)){
playerMove = Vector3(-1,0,0);
}
else if(Input.GetKey(KeyCode.RightArrow)){
playerMove = Vector3(1,0,0);
}
else{
playerMove = Vector3(0,0,0);
}
[snip...]
...so i will always get a value passed to 'playerMove'. If it wasn't for you pointing me in the right direction i'd of been stuck on that for a while!
Thanks a lot! :)