##わからないこと
キャラ移動させる際にLookRotation()でキャラの向きを指定しているのですが、立ち止まった際にZ方向へ向きを元に戻してしまいます。なぜそうなってしまったのかこちらではこれ以上考察の余地がなくなってしまったのでこちらに投稿させていただきました。よろしくお願いします。以下、キャラクターを移動させるときに書いたコードです。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GirlMove : MonoBehaviour { 6 7 public float walkSpeed; 8 public float runSpeed; 9 10 private Vector3 moveDirection = Vector3.zero; 11 private float x; 12 private float z; 13 private float gravity = 98f; 14 private CharacterController controller; 15 16 17 18 // Use this for initialization 19 void Start () { 20 controller = GetComponent<CharacterController>(); 21 } 22 23 // Update is called once per frame 24 void Update () { 25 26 x = Input.GetAxis("Horizontal"); 27 z = Input.GetAxis("Vertical"); 28 29 if(Input.GetMouseButton(1)) { 30 moveDirection = new Vector3(x * runSpeed, 0, z * runSpeed); 31 } else { 32 moveDirection = new Vector3(x * walkSpeed, 0, z * walkSpeed); 33 } 34 35 36 37 if(controller.isGrounded) { 38 moveDirection.y = 0f; 39 } else { 40 moveDirection.y -= gravity * Time.deltaTime; 41 } 42 43 controller.Move(moveDirection * Time.deltaTime); 44 45 Quaternion q = Quaternion.LookRotation(new Vector3(moveDirection.x, 0, moveDirection.z)); 46 transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 1200.0f * Time.deltaTime); 47 48 } 49 50 void OnControllerColliderHit(ControllerColliderHit col) { 51 col.gameObject.GetComponent<Rigidbody>().AddForce(transform.forward * 200f); 52 } 53} 54
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/19 07:02
2018/09/19 07:17
2018/09/19 07:43