I have 2 questions about moving in unity3d
first of all what i have to change in my update method to make character able to move whether is he grounded or not, if i try to do it i always make something wrong so it just keep jumping even if he havent touched the ground heres my update method
function Update () {
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical"))
moveDirection *= .7;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection == Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
moveDirection.y = jumpSpeed;
if(transform.position.y > highestPosition) highestPosition = transform.position.y;
GameObject.Find("g_Score").guiText.text = " "+highestPosition;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(2) || Input.GetMouseButton(0))
Screen.lockCursor = false;
else
Screen.lockCursor = false;
// Toggle walking/running with the T key
if(Input.GetKeyDown("t"))
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller : CharacterController = GetComponent(CharacterController);
var flags : CollisionFlags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
and then for the another question, how can i make the moving forward and back with keys right and left instead of up and down, please help me someone, i am so pissed off with this! :<