-
-
Save pillar/dbda67fd61b98fb13be8ffed98646f79 to your computer and use it in GitHub Desktop.
Unity Character Controller 移動
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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