現在unityちゃんを使って移動をしています。
3Dゲーム開発講座を使っているのですが、
移動方法がワールド座標での方向固定でカメラをキャラに追尾させても
歩く方向はやはりかわりません
どこにいても
右を押したら右に、
上を押したら画面奥に
下を押したら画面手前側に、
左を押したら左に移動する方法をおしえてください!
+++++++++++++++++++追記++++++++++++++++
調べてここまでは作成することができました。
前左右は問題ないのですが、後ろだけワールド座標の動きになり、かならず同じ向きに下がります。
それに加えバックするときだけがたがたします。
lang
1using UnityEngine; 2using System.Collections; 3 4public class Player : MonoBehaviour { 5 6 Vector3 direction; 7 //移動速度 8 public float move_speed = 5f; 9 //回転速度 10 public float rotate_speed = 180f; 11 //ジャンプ速度 12 public float jump_speed = 5f; 13 //重力 14 private float gravity=20f; 15 //アニメーターコンポーネント 16 Animator anim; 17 //キャラコントローラー 18 CharacterController chara; 19 20 21 Transform cam_trans; 22 // Use this for initialization 23 void Start () { 24 chara = GetComponent<CharacterController>(); 25 anim = GetComponentInChildren<Animator>(); 26 cam_trans = GameObject.Find("Main Camera").GetComponent<Transform>(); 27 } 28 29 // Update is called once per frame 30 void Update () { 31 32 if (chara.isGrounded) 33 { 34 35 // direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); 36 direction = (cam_trans.transform.right * Input.GetAxis("Horizontal")) + 37 (cam_trans.transform.forward * Input.GetAxis("Vertical")); 38 39 40 if (direction.sqrMagnitude > 0.01f) 41 { 42 Vector3 forward = Vector3.Slerp(transform.forward, direction, rotate_speed * Time.deltaTime / Vector3.Angle(transform.forward, direction)); 43 transform.LookAt(transform.position + forward); 44 } 45 46 if (Input.GetKeyDown(KeyCode.Space)) 47 { 48 direction.y = jump_speed; 49 } 50 } 51 52 direction.y -= gravity * Time.deltaTime; 53 54 chara.Move(direction * Time.deltaTime * move_speed); 55 anim.SetFloat("Speed", chara.velocity.magnitude); 56 } 57 58} 59
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/02/26 13:25