Skip to content

Instantly share code, notes, and snippets.

@TORISOUP
Created January 11, 2015 16:16
Show Gist options
  • Save TORISOUP/6262312e9727acb5976b to your computer and use it in GitHub Desktop.
Save TORISOUP/6262312e9727acb5976b to your computer and use it in GitHub Desktop.
CharacterControllerとMoveの例
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