Last active
August 29, 2015 14:15
-
-
Save CosmoCleaner/cac28b6218d648c27c50 to your computer and use it in GitHub Desktop.
Unityでバーチャルジョイスティックからアニメーションを操作する簡易スクリプト
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 UnitySampleAssets.CrossPlatformInput; | |
public class JoystickToMove : MonoBehaviour | |
{ | |
// ジョイスティックの値にアニメーションを追従させるスピード | |
const float ADJUST_SPEED = 0.05f; | |
Animator anim; | |
// Animator の Parameters に入力する値 | |
float forward, turn; | |
void Awake() | |
{ | |
anim = GetComponent<Animator>(); | |
} | |
void Update() | |
{ | |
// バーチャルジョイスティックから値を取得 | |
float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal"); | |
float v = CrossPlatformInputManager.GetAxisRaw ("Vertical"); | |
// 縦軸の値が負だったらとにかくターンさせる、ための処理 | |
if (v < 0) | |
{ | |
v = 0; | |
h = h < 0 ? -1 : 1; | |
} | |
// 滑らかにアニメーション遷移するためのちょっとした工夫 | |
// 例えばジョイスティックの値がいきなり1から0になっても滑らかに0に近づく。 | |
forward += (v - forward) * ADJUST_SPEED; | |
turn += (h - turn) * ADJUST_SPEED; | |
// Animator に値を入力 | |
anim.SetFloat("Forward", forward); | |
anim.SetFloat("Turn", turn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment