Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created November 16, 2013 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kankikuchi/7499555 to your computer and use it in GitHub Desktop.
Save kankikuchi/7499555 to your computer and use it in GitHub Desktop.
Unity Character Controller 移動
public float walkSpeed = 7.0f; //歩く速度
public float gravity = 10.0f;//重力加速度
private Vector3 velocity;//現在の速度
void Update () {
//CharacterControllerを取得
CharacterController controller = GetComponent<CharacterController>();
float SpeedX = 0f, SpeedY = -gravity, SpeedZ = 0f;
/////キー入力確認 各キーが押されているか
if (Input.GetKey(KeyCode.A)){
SpeedX = -1f;
}
else if (Input.GetKey(KeyCode.D)){
SpeedX = 1f;
}
if (Input.GetKey(KeyCode.W)){
SpeedZ = 1f;
}
else if (Input.GetKey(KeyCode.S)){
SpeedZ = -1f;
}
velocity = new Vector3( SpeedX , SpeedY , SpeedZ );
//キャラクターコントローラーの移動
controller.Move(velocity * walkSpeed * Time.deltaTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment