ゾンビFPS_ゾンビへの銃撃
+ボタン押下でコードを確認できます!
コピペしても正しく動かない場合は
エディタでアタッチし忘れている可能性が高いです。
- 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; GameObject target; public float runSpeed; public int attackDamage; // Start is called before the first frame update void Start() { animator = GetComponent<Animator>(); agent = GetComponent<NavMeshAgent>(); if(target == null) { target = GameObject.FindGameObjectWithTag("Player"); } } public void TurnOffTrigger() { animator.SetBool("Walk", false); animator.SetBool("Run", false); animator.SetBool("Death", false); animator.SetBool("Attack", false); } float DistanceToPlayer() { if(GameState.GameOver) { return Mathf.Infinity; } return Vector3.Distance(target.transform.position, transform.position); } bool CanSeePlayer() { if (DistanceToPlayer() < 15) { return true; } return false; } bool ForGetPlayer() { if (DistanceToPlayer() > 20) { return true; } return false; } public void DamagePlayer() { if (target != null) { target.GetComponent<FPSController>().TakeHit(attackDamage); } } public void ZombieDeath() { TurnOffTrigger(); animator.SetBool("Death", true); state = STATE.DEAD; } // 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; } 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(); } if (CanSeePlayer()) { state = STATE.CHASE; } break; case STATE.CHASE: if(GameState.GameOver) { TurnOffTrigger(); agent.ResetPath(); state = STATE.WANDER; return; } agent.SetDestination(target.transform.position); agent.stoppingDistance = 3; TurnOffTrigger(); agent.speed = runSpeed; animator.SetBool("Run", true); if (agent.remainingDistance <= agent.stoppingDistance) { state = STATE.ATTACK; } if (ForGetPlayer()) { agent.ResetPath(); state = STATE.WANDER; } break; case STATE.ATTACK: if (GameState.GameOver) { TurnOffTrigger(); agent.ResetPath(); state = STATE.WANDER; return; } TurnOffTrigger(); animator.SetBool("Attack", true); transform.LookAt(new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z)); if (DistanceToPlayer() > agent.stoppingDistance + 2) { state = STATE.CHASE; } break; case STATE.DEAD: Destroy(agent); break; } } }
- Weapon
- 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; public class Weapon : MonoBehaviour { public AudioSource weapon; public AudioClip relodingSE, fireSE, triggerSE; public static Weapon instance; public Transform shotDirection; private void Awake() { if (instance == null) { instance = this; } } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { //開発が終了したらコメントアウトか削除すること Debug.DrawRay(shotDirection.position, shotDirection.transform.forward * 10, Color.green); } public void CanShoot() { GameState.canShoot = true; } public void FireSE() { weapon.clip = fireSE; weapon.Play(); } public void RelodingSE() { weapon.clip = relodingSE; weapon.Play(); } public void TriggerSE() { if (!weapon.isPlaying) { weapon.clip = triggerSE; weapon.Play(); } } public void Shooting() { RaycastHit hitInfo; if (Physics.Raycast(shotDirection.transform.position, shotDirection.transform.forward, out hitInfo, 300)) { if (hitInfo.collider.gameObject.GetComponent<ZombieController>() != null) { ZombieController hitZombie = hitInfo.collider.gameObject.GetComponent<ZombieController>(); hitZombie.ZombieDeath(); } } } }
他の講座クーポン【93%OFF】
関連記事
クーポン期限2023/01/24 Udemyでは定価24000円になっていますがこのクーポンページからの購入で1800円台で購入することが可能になっています! 30日以内な[…]
Udemyよりお得に講座視聴可能なYouTubeメンバーシップ