走るモーションを使って3dアクションゲームような動きである押した方向にキャラクターの向きが変わって進んでいく処理のプログラムを書きたいのが回転をさせる処理がわかりません。
例えば進行方向と逆のキーを押したらそのキーの向きにが変わってその向きに走りだすといった動きを作りたいです。教えてくれますでしょうか?
c#
1public class UnichanController : MonoBehaviour { 2 private Animator anim; 3 private Rigidbody rb; 4 //private Vector3 targetposition = new Vector3(); 5 private Transform target; 6 Vector3 targetposition; 7 Vector3 Player_pos = new Vector3(); 8 // Use this for initialization 9 void Start () 10 { 11 12 anim = GetComponent<Animator>(); 13 rb = GetComponent<Rigidbody>(); 14 Player_pos = GetComponent<Transform>().position; 15 16 17 } 18 19 void FixedUpdate() 20 { 21 22 23 } 24 25 int key_z = 0; 26 int key_x = 0; 27 28 29 // Update is called once per frame 30 void Update () 31 { 32 key_z = 0; 33 if(Input.GetKey(KeyCode.UpArrow)) 34 { 35 36 key_z = 1; 37 38 anim.SetBool("Run",true); 39 40 } 41 42 43 if(Input.GetKey(KeyCode.DownArrow)) 44 { 45 key_z = -1; 46 47 anim.SetBool("Run",true); 48 } 49 50 rb.velocity = new Vector3(0,0,key_z); 51 Vector3 diff = transform.position - Player_pos; //プレイヤーがどの方向に進んでいるかがわかるように、初期位置と現在地の座標差分を取得 52 53 if (diff.magnitude > 0.01f) //ベクトルの長さが0.01fより大きい場合にプレイヤーの向きを変える処理を入れる(0では入れないので) 54 { 55 transform.rotation = Quaternion.LookRotation(diff); //ベクトルの情報をQuaternion.LookRotationに引き渡し回転量を取得しプレイヤーを回転させる 56 } 57 58 Player_pos = transform.position; //プレイヤーの位置を更新 59 if(key_z == 0) 60 { 61 anim.SetBool("Run",false); 62 63 } 64 65 66 67 } 68 69 70} 71
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/04/01 06:18
2019/04/01 06:28
退会済みユーザー
2019/04/01 06:40
2019/04/01 08:38
退会済みユーザー
2019/04/01 11:25