Created
January 11, 2015 16:16
-
-
Save TORISOUP/6262312e9727acb5976b to your computer and use it in GitHub Desktop.
CharacterControllerとMoveの例
This file contains 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
using UnityEngine; | |
using System.Collections; | |
public class PlayerMoveUseMove : MonoBehaviour | |
{ | |
private CharacterController characterController; | |
public float speed = 3.0f; | |
void Start() | |
{ | |
this.characterController = GetComponent<CharacterController>(); | |
} | |
//毎フレーム実行される | |
void Update() | |
{ | |
if (this.characterController == null) | |
{ | |
return; | |
} | |
//移動キーの入力を受け付ける | |
float inputHorizontal = Input.GetAxisRaw("Horizontal"); | |
float inputVertical = Input.GetAxisRaw("Vertical"); | |
//速度ベクトルを作る | |
Vector3 velocity = new Vector3(inputHorizontal, 0, inputVertical); | |
//移動速度を調整する | |
velocity *= speed; | |
//重力加速度を加える(現在の速度+重力加速度の加速分) | |
//SetYは拡張メソッド | |
velocity = velocity.SetY(this.characterController.velocity.y + Physics.gravity.y * Time.deltaTime); | |
//速度にTime.deltaTimeを書けて移動量にする | |
this.characterController.Move(velocity * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment