ゾンビAI_さまよう
+ボタン押下でコードを確認できます!
コピペしても正しく動かない場合は
エディタでアタッチし忘れている可能性が高いです。
- ZombieController
- This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class ZombieController : MonoBehaviour { Animator animator; NavMeshAgent agent; public float walkingSpeed; enum STATE { IDLE, WANDER, ATTACK, CHASE, DEAD }; STATE state = STATE.IDLE; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); agent = GetComponent<NavMeshAgent>(); } public void TurnOffTrigger() { animator.SetBool("Walk", false); animator.SetBool("Run", false); animator.SetBool("Death", false); animator.SetBool("Attack", false); } // Update is called once per frame void Update() { switch (state) { case STATE.IDLE: TurnOffTrigger(); if (CanSeePlayer()) { state = STATE.CHASE; } else if (Random.Range(0, 5000) < 5) { state = STATE.WANDER; } break; case STATE.WANDER: if (!agent.hasPath) { float newX = transform.position.x + Random.Range(-5, 5); float newZ = transform.position.z + Random.Range(-5, 5); Vector3 NextPos = new Vector3(newX, transform.position.y, newZ); agent.SetDestination(NextPos); agent.stoppingDistance = 0; TurnOffTrigger(); agent.speed = walkingSpeed; animator.SetBool("Walk", true); } if (Random.Range(0, 5000) < 5) { state = STATE.IDLE; agent.ResetPath(); } break; } } }
他の講座クーポン【93%OFF】
関連記事
クーポン期限2023/01/24 Udemyでは定価24000円になっていますがこのクーポンページからの購入で1800円台で購入することが可能になっています! 30日以内な[…]
Udemyよりお得に講座視聴可能なYouTubeメンバーシップ