Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Last active December 28, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kankikuchi/7502135 to your computer and use it in GitHub Desktop.
Save kankikuchi/7502135 to your computer and use it in GitHub Desktop.
Unity Character Controller バーチャルパッド移動
public float walkSpeed = 15.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;
}
//バーチャルパッドのオブジェクト取得 オブジェクト名 : Joystick
GameObject Joystick = GameObject.Find("Joystick");
//バーチャルパッドのオブジェクトのスクリプトを取得 スクリプト名 : MpJoystick
MPJoystick JoystickScript = Joystick.GetComponent<MPJoystick>();
//パーチャルパッドがタップされている マウスではタップ出来ない
if(JoystickScript.tapCount > 0){
//スピードを設定
SpeedX = JoystickScript.position.x;
SpeedZ = JoystickScript.position.y;
}
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