Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created November 19, 2013 00:23
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/7537895 to your computer and use it in GitHub Desktop.
Save kankikuchi/7537895 to your computer and use it in GitHub Desktop.
Unity 視点移動(キーボード or タッチ)2
//キャラクター回転
void PlayerRotation(){
//回転速度
float RotX = 0f , RotY = 0f;
/////キー入力確認 各キーが押されているか
if (Input.GetKey(KeyCode.LeftArrow)){
RotY = -1f;
}
else if (Input.GetKey(KeyCode.RightArrow)){
RotY = 1f;
}
if (Input.GetKey(KeyCode.UpArrow)){
RotX = -1f;
}
else if (Input.GetKey(KeyCode.DownArrow)){
RotX = 1f;
}
//バーチャルパッドのオブジェクト取得 オブジェクト名 : Joystick
GameObject Joystick = GameObject.Find("Joystick");
//バーチャルパッドのオブジェクトのGUITextureを取得
GUITexture JoystickGUITexture = Joystick.GetComponent<GUITexture>();
//タップされている
if(Input.touchCount > 0){
//タッチされている指の数だけループ
foreach ( Touch touch in Input.touches ){
//JoystickのGUITextureに触れていない
if(!JoystickGUITexture.HitTest(touch.position)){
//指が移動した分だけ画面を回転
RotX = -touch.deltaPosition.y * RotTouchSpeed;
RotY = touch.deltaPosition.x * RotTouchSpeed;
}
}
}
//回転予定角度X
float NextRotX = transform.eulerAngles.x + RotX * RotSpeed *Time.deltaTime;
//x方向の回転を制限 回転可能角度外
if(NextRotX > LimitRotX && NextRotX < 360f - LimitRotX){
//下と上のどちらから可能角度を超えたか それに応じて制限
NextRotX = NextRotX > 180f ? 360f - LimitRotX : LimitRotX;
}
//回転
transform.rotation = Quaternion.Euler(
NextRotX,
transform.eulerAngles.y + RotY * RotSpeed *Time.deltaTime ,
transform.eulerAngles.z
) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment