敵キャラクターが攻撃してくれません
↓参考にさせていただいているサイト
https://gametukurikata.com/program/attacknear
↓敵のスクリプトです
C#
1using UnityEngine; 2using System.Collections; 3using UnityEngine.AI; 4 5public class Enemy : MonoBehaviour 6{ 7 8 public enum EnemyState 9 { 10 Walk, 11 Wait, 12 Chase, 13 Attack, 14 Freeze 15 }; 16 17 #region プレイべーと変数 18 private CharacterController enemyController; 19 private Animator animator; 20 // 目的地 21 private Vector3 destination; 22 // 歩くスピード 23 [SerializeField] 24 private float walkSpeed = 1.0f; 25 // 速度 26 private Vector3 velocity; 27 // 移動方向 28 private Vector3 direction; 29 // 到着フラグ 30 private bool arrived; 31 // SetPositionスクリプト 32 private SetPosition setPosition; 33 // 待ち時間 34 [SerializeField] 35 private float waitTime = 5f; 36 // 経過時間 37 private float elapsedTime; 38 // 敵の状態 39 private EnemyState state; 40 // 追いかけるキャラクター 41 private Transform playerTransform; 42 // エージェント 43 private NavMeshAgent navMeshAgent; 44 // 回転スピード 45 [SerializeField] 46 private float rotateSpeed; 47 // 攻撃した後のフリーズ状態 48 [SerializeField] 49 private float freezeTime = 0.5f; 50 #endregion 51 52 // Use this for initialization 53 void Start() 54 { 55 navMeshAgent = GetComponent<NavMeshAgent>(); 56 enemyController = GetComponent<CharacterController>(); 57 animator = GetComponent<Animator>(); 58 setPosition = GetComponent<SetPosition>(); 59 setPosition.CreateRondomPosition(); 60 destination = setPosition.GetDestination(); 61 velocity = Vector3.zero; 62 arrived = false; 63 elapsedTime = 0f; 64 SetState(EnemyState.Walk); 65 } 66 67 // Update is called once per frame 68 void Update() 69 { 70 // 見回りまたはキャラクターを追いかける状態 71 if (state == EnemyState.Walk || state == EnemyState.Chase) 72 { 73 // キャラクターを追いかける状態であればキャラクターの目的地を再設定 74 if (state == EnemyState.Chase) 75 { 76 setPosition.SetDestination(playerTransform.position); 77 } 78 if (enemyController.isGrounded) 79 { 80 velocity = Vector3.zero; 81 animator.SetFloat("Speed", 2.0f); 82 direction = (setPosition.GetDestination() - transform.position).normalized; 83 transform.LookAt(new Vector3(setPosition.GetDestination().x, transform.position.y, setPosition.GetDestination().z)); 84 velocity = direction * walkSpeed; 85 } 86 if (state == EnemyState.Walk) 87 { 88 // 目的地に到着したかどうかの判定 89 if (Vector3.Distance(transform.position, setPosition.GetDestination()) < 0.7f) 90 { 91 SetState(EnemyState.Wait); 92 animator.SetFloat("Speed", 0.0f); 93 } 94 } 95 else if(state == EnemyState.Walk) 96 { 97 // 攻撃する距離だったら攻撃 98 if(Vector3.Distance(transform.position,setPosition.GetDestination()) < 1f) 99 { 100 SetState(EnemyState.Attack); 101 } 102 } 103 104 }// 到着していたら一定時間待つ 105 else if (state == EnemyState.Wait) 106 { 107 elapsedTime += Time.deltaTime; 108 109 // 待ち時間を越えたら次の目的地を設定 110 if (elapsedTime > waitTime) 111 { 112 SetState(EnemyState.Walk); 113 } 114 } 115 // 攻撃後のフリーズ状態 116 else if(state == EnemyState.Freeze) 117 { 118 elapsedTime += Time.deltaTime; 119 if(elapsedTime > freezeTime) 120 { 121 SetState(EnemyState.Walk); 122 } 123 } 124 velocity.y += Physics.gravity.y * Time.deltaTime; 125 enemyController.Move(velocity * Time.deltaTime); 126 } 127 128 //if(state == EnemyState.Dead) 129 //{ 130 // return; 131 //} 132 133 // if (!arrived) 134 // { 135 // if (enemyController.isGrounded) 136 // { 137 // velocity = Vector3.zero; 138 // animator.SetFloat("Speed", 2.0f); 139 // direction = (destination - transform.position).normalized; 140 // transform.LookAt(new Vector3(destination.x, transform.position.y, destination.z)); 141 // velocity = direction * walkSpeed; 142 // Debug.Log(destination); 143 // } 144 // velocity.y += Physics.gravity.y * Time.deltaTime; 145 // enemyController.Move(velocity * Time.deltaTime); 146 147 // // 目的地に到着したかどうかの判定 148 // if (Vector3.Distance(transform.position, destination) < 0.5f) 149 // { 150 // arrived = true; 151 // animator.SetFloat("Speed", 0.0f); 152 // } 153 // // 到着していたら 154 // } 155 // else 156 // { 157 // elapsedTime += Time.deltaTime; 158 159 // // 待ち時間を越えたら次の目的地を設定 160 // if (elapsedTime > waitTime) 161 // { 162 // setPosition.CreateRondomPosition(); 163 // destination = setPosition.GetDestination(); 164 // arrived = false; 165 // elapsedTime = 0f; 166 // } 167 // Debug.Log(elapsedTime); 168 // } 169 public void SetState(EnemyState tempState, Transform targetObj = null) 170 { 171 if (tempState == EnemyState.Walk) 172 { 173 arrived = false; 174 elapsedTime = 0f; 175 state = tempState; 176 setPosition.CreateRondomPosition(); 177 } 178 else if (tempState == EnemyState.Chase) 179 { 180 state = tempState; 181 // 待機状態から追いかける場合もあるのでOFF 182 arrived = false; 183 // 追いかける対象をセット 184 playerTransform = targetObj; 185 } 186 else if (tempState == EnemyState.Wait) 187 { 188 elapsedTime = 0f; 189 state = tempState; 190 arrived = true; 191 velocity = Vector3.zero; 192 animator.SetFloat("Speed", 0f); 193 } 194 else if(tempState == EnemyState.Attack) 195 { 196 velocity = Vector3.zero; 197 animator.SetFloat("Speed", 0f); 198 animator.SetBool("Attack", true); 199 } 200 else if(tempState == EnemyState.Freeze) 201 { 202 elapsedTime = 0f; 203 velocity = Vector3.zero; 204 animator.SetFloat("Speed", 0f); 205 animator.SetBool("Attack", false); 206 } 207 } 208 // 敵キャラクターの状態取得メソッド 209 public EnemyState GetState() 210 { 211 return state; 212 } 213} 214 215 216
Unity上でのエラー
なし
プレイヤーのスクリプト
C#
1using UnityEngine; 2using System.Collections; 3 4public class Move : MonoBehaviour 5{ 6 public enum MyState 7 { 8 Normal, 9 Damage 10 }; 11 12 private MyState state; 13 private CharacterController characterController; 14 private Vector3 velocity; 15 [SerializeField] 16 private float walkSpeed; 17 private Animator animator; 18 19 // Use this for initialization 20 void Start() 21 { 22 characterController = GetComponent<CharacterController>(); 23 animator = GetComponent<Animator>(); 24 } 25 26 // Update is called once per frame 27 void Update() 28 { 29 if (state == MyState.Normal) 30 { // 地面に設置してるときは速度を初期化 31 if (characterController.isGrounded) 32 { 33 velocity = Vector3.zero; 34 var input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); 35 36 if (input.magnitude > 0.1f) 37 { 38 animator.SetFloat("Speed", input.magnitude); 39 transform.LookAt(transform.position + input); 40 velocity += input.normalized * walkSpeed; 41 } 42 else 43 { 44 animator.SetFloat("Speed", 0f); 45 } 46 } 47 } 48 velocity.y += Physics.gravity.y * Time.deltaTime; 49 characterController.Move(velocity * Time.deltaTime); 50 } 51 52 public void TakeDamage(Transform enemyTransform) 53 { 54 state = MyState.Damage; 55 velocity = Vector3.zero; 56 animator.SetTrigger("Damage"); 57 // characterController.Move(enemyTransform.forward * 0.5f); 58 } 59}
書いてみてもエラーが取れません。
それか、ここ間違ってるとか、、書いてあるとか、あれば教えてください。
ご親切な方、処理の書き方を教えてもらえませんか?
回答お願いします。
やった事
AnimationのRigなどをいじりましたが、攻撃してきません。主人公キャラも右手を挙げています
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/05 07:04
2020/09/07 00:29
2020/09/08 12:50 編集