unityで攻撃中に移動ができてしまうので困っています。
改善策を教えてください。
コードは「攻撃」と「移動」の二つに分けて使って、「攻撃」ではSetBoolと書いていますがAnimatorではTriggerを使っています。
攻撃アニメーター 攻撃コード
移動アニメーター+移動コード
上記の記事を参考に作りました。
長くてすいません...
「攻撃」 スクリプト名[Attack]
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Attack : MonoBehaviour 6{ 7 //PlayerのAnimatorコンポーネント保存用 8 private Animator animator; 9 10 // Use this for initialization 11 void Start() 12 { 13 //PlayerのAnimatorコンポーネントを取得する 14 animator = GetComponent<Animator>(); 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 21 //左クリックで攻撃 22 if (Input.GetButtonDown("Fire1")) 23 { 24 animator.SetBool("Attack", true); 25 } 26 27 } 28 29}
「移動」 スクリプト名[Move]
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Move : MonoBehaviour 6{ 7 public Animator anim; 8 public bool isGround; 9 public RaycastHit hit; 10 11 public float InputHorizontal; 12 public float InputVertical; 13 public Rigidbody rb; 14 public float moveSpeed; 15 public GameObject Cam; 16 public Vector3 Cam_forward; 17 public Vector3 move_forward; 18 public Vector3 InputMagnitude; 19 20 void Start() 21 { 22 anim = this.gameObject.GetComponent<Animator>(); 23 rb = this.gameObject.GetComponent<Rigidbody>(); 24 } 25 26 void Update() 27 { 28 InputHorizontal = Input.GetAxis("Horizontal"); //横入力 29 InputVertical = Input.GetAxis("Vertical"); //縦入力 30 31 InputMagnitude = new Vector3(InputHorizontal, 0, InputVertical); 32 33 //移動 34 35 if (InputMagnitude.magnitude > 0.001f) 36 { 37 anim.SetFloat("Speed", InputMagnitude.magnitude); 38 } 39 else 40 { 41 anim.SetFloat("Speed", 0); 42 } 43 44 45 46 Cam_forward = Vector3.Scale(Cam.transform.forward, new Vector3(1, 0, 1)).normalized; 47 48 move_forward = Cam_forward * InputVertical + Cam.transform.right * InputHorizontal; 49 50 rb.velocity = move_forward * moveSpeed + new Vector3(0, rb.velocity.y, 0); 51 52 53 if (move_forward != Vector3.zero) 54 { 55 this.transform.rotation = Quaternion.LookRotation(move_forward); 56 } 57 58 } 59}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 14:50
2019/05/08 15:00
2019/05/09 12:31
2019/05/09 12:42
2019/05/09 14:27