前提・実現したいこと
キャラクターは2D、フィールドは3Dのゲームを作っています
プレイヤーの押した位置に対応したアニメーションの実装
発生している問題・エラーメッセージ
プレイヤーより右をクリックしたら右のアニメーション、プレイヤーより左をクリックしたら左のアニメーション のような処理を書いているのですがプレイヤーから左上、左下、右上、右下 を押したときに左のアニメーションと上のアニメーションが被ってしまいます。 奥なら奥、右なら右、下なら下、手前なら手前のアニメーションを行いたいのですがどのようにすればいいでしょうか?
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine.AI; 4using UnityEngine; 5 6 7public class PlayerAnimMove : MonoBehaviour 8{ 9 private Vector3 targetPosition; //移動する位置 10 private NavMeshAgent agent; 11 private Animator animator; 12 private RaycastHit hit; 13 private Ray ray; 14 public float count; 15 16 private const string Trigger_isRight = "is_Right"; 17 private const string Trigger_isLeft = "is_Left"; 18 private const string Trigger_isWait = "is_Wait"; 19 private const string Trigger_isBefor = "is_Befor"; 20 private const string Trigger_isAfter = "is_After"; 21 22 23 void Start() 24 { 25 agent = GetComponent<NavMeshAgent>(); 26 animator = GetComponent<Animator>();//後から変更 27 this.animator.SetTrigger(Trigger_isWait); 28 } 29 30 void Update() 31 { 32 if (Input.GetMouseButton(0)) 33 { 34 //count += Time.deltaTime; 35 36 Debug.Log("anim"); 37 ray = Camera.main.ScreenPointToRay(Input.mousePosition); 38 if (Physics.Raycast(ray, out hit, 100)) 39 { 40 targetPosition = hit.point; 41 //agent.SetDestination(targetPosition); //移動させる処理 42 43 if (transform.position.x > targetPosition.x) //プレイヤーの位置よりもクリック位置が左のときに実行 44 { 45 animator.SetFloat("speed", agent.velocity.magnitude); 46 this.animator.SetTrigger(Trigger_isLeft); 47 Debug.Log("hidari"); 48 } 49 50 if (transform.position.x < targetPosition.x) //プレイヤーの位置よりもクリック位置が右のときに実行 51 { 52 animator.SetFloat("speed", agent.velocity.magnitude); 53 this.animator.SetTrigger(Trigger_isRight); 54 Debug.Log("migi"); 55 } 56 57 if (transform.position.z > targetPosition.z) //プレイヤーの位置よりもクリック位置が奥のときに実行 58 { 59 animator.SetFloat("speed", agent.velocity.magnitude); 60 this.animator.SetTrigger(Trigger_isAfter); 61 Debug.Log("oku"); 62 } 63 64 if (transform.position.z < targetPosition.z) //プレイヤーの位置よりもクリック位置が手前のときに実行 65 { 66 animator.SetFloat("speed", agent.velocity.magnitude); 67 this.animator.SetTrigger(Trigger_isBefor); 68 Debug.Log("temae"); 69 } 70 71 } 72 73 if (transform.position == targetPosition) //押した位置と今いる位置が同じなら実行 74 { 75 this.animator.SetTrigger(Trigger_isWait); 76 } 77 else 78 { 79 //agent.SetDestination(targetPosition); //移動させる処理 80 } 81 } 82 } 83}
試したこと
プレイヤーから見て右、左、上、下のトリガーでそれぞれのアニメーションになっている所まではできました
調べた所、角度で求めると実装出来ると思い atan2 というものを使うと出来る事が分かったが使い方がよく分からず
補足情報(FW/ツールのバージョンなど)
Unity 2017.3.1f1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。