Last active
December 28, 2015 16:09
-
-
Save kankikuchi/7526680 to your computer and use it in GitHub Desktop.
Unity 視点移動(キーボード or タッチ)
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
//キャラクター回転 | |
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; | |
} | |
//タッチされている | |
if(Input.touchCount > 0){ | |
//タッチされている指の数だけループ | |
foreach ( Touch touch in Input.touches ){ | |
//画面の半分より上 | |
if(touch.position.y > Screen.height * 0.5f){ | |
//指が移動した分だけ画面を回転 | |
RotX = -touch.deltaPosition.y; | |
RotY = touch.deltaPosition.x; | |
} | |
} | |
} | |
//回転予定角度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