Advertisement
Albert3069

미완성 AI Script 1003

Oct 3rd, 2021
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. using UnityEngine;
  7. using UnityEngine.AI;
  8. using Vector3 = UnityEngine.Vector3;
  9.  
  10. public class AI : MonoBehaviour
  11. {
  12.     // Start is called before the first frame update
  13.     PC_Character Character;
  14.     PC_CharacterStats CharacterStats;
  15.  
  16.     NavMeshAgent navMeshAgent;
  17.     Animator animator;
  18.  
  19.     public GameObject Enemy;    //적 오브젝트 (플레이어)
  20.     public GameObject AIself = new GameObject();   //일단 거리계산을 위해 AI 오브젝트도 따로 만듦
  21.     Vector3 EnemyDir;
  22.     Vector3 AvoidPosition = new Vector3(0.9f, 6.7f, 25);    //미리 설정한 회피 위치
  23.     Vector3 TestPositon = new Vector3(-5.8f, 7, -10);   //미리 설정한 위치
  24.     Vector3 CenterPosition = new Vector3(-6, 6.5f, 6);  //미리 설정한 센터 위치
  25.  
  26.     private float Dist; //플레이어와 적 거리 계산해 넣을 변수
  27.  
  28.     void Start()
  29.     {
  30.         Character = GetComponent<PC_Character>();
  31.         CharacterStats = GetComponent<PC_CharacterStats>();
  32.  
  33.         navMeshAgent = GetComponent<NavMeshAgent>();
  34.         animator = GetComponent<Animator>();
  35.     }
  36.  
  37.     // Update is called once per frame
  38.     void Update()
  39.     {
  40.         Dist = Vector3.Distance(AIself.transform.position, Enemy.transform.position);   //플레이어와 적 오브젝트 간의 거리 계산
  41.        
  42.         if (Enemy != null && !Enemy.activeSelf) Enemy = null;   // 적이 죽었을시 타켓 초기화
  43.  
  44.         if (Enemy == null)
  45.         {
  46.             animator.SetBool("isMove", true);
  47.             navMeshAgent.SetDestination(CenterPosition);    //적이 없으면 센터로 이동
  48.         }
  49.  
  50.         if (Enemy != null && !Enemy.GetComponent<PC_CharacterStats>().InGrass)  // 적이 부쉬에 없을시
  51.         {
  52.             if (CharacterStats.HP < Enemy.GetComponent<PC_CharacterStats>().HP)
  53.             {
  54.                 //체력이 적보다 적을 때 취하는 행동?
  55.                 //일단 도망가는 AI 개선이 필요하다.
  56.                 animator.SetBool("isMove", true);
  57.                 navMeshAgent.stoppingDistance = 0.5f;
  58.                 if (gameObject.CompareTag("Enemy")) navMeshAgent.SetDestination(AvoidPosition);
  59.                 else if(gameObject.CompareTag("Friendly")) navMeshAgent.SetDestination(TestPositon);
  60.             }
  61.             else
  62.             {
  63.                 //적보다 체력이 불리하지 않을 때 취하는 행동
  64.                 animator.SetBool("isMove", true);
  65.                 navMeshAgent.stoppingDistance = 5;  //적과 5의 거리 유지?
  66.                 navMeshAgent.SetDestination(Enemy.transform.position);
  67.                
  68.             }
  69.            
  70.             EnemyDir = Enemy.transform.position - transform.position;   //적의 위치와 나의 위치 상대 연산?
  71.             Character.point = new Vector3(EnemyDir.x, 0, EnemyDir.z);
  72.  
  73.             if (CharacterStats.ReadyUltiFire) Character.UltimateAttack();   //궁극기 충전 - 궁극기 나감
  74.             else Character.BasicAttack();   //그렇지 않다면 그냥 일반 공격을 함
  75.         }
  76.         else if (CharacterStats.HP <= 100) navMeshAgent.SetDestination(AvoidPosition);  
  77.         //적이 부쉬에 있고, 캐릭터 HP가 100보다 작다면 회피 포지션으로 목적지 설정
  78.        
  79.     }
  80.     //목적지가 Enemy.transform.position일 때(추적 중일 때) 플레이어가 부쉬에 들어간다면
  81.     //&& 그리고 플레이어가 AI의 식별 거리 내에 있다면 ==> 두 조건 모두 만족 시 플레이어가 부쉬로 들어간 위치 찍고 추적하는 구문 제작해야함
  82.     private void OnTriggerEnter(Collider collision) //플레이어가 트리거 안에 들어갔을 때 실행되는 메소드
  83.     {
  84.         if (Dist <= 6 && collision.CompareTag("Grass")) //플레이어와 적 간 거리 6 이내이고, 플레이어가 들어간 곳이 부쉬일 때
  85.         {
  86.             Vector3 spotPos=AIself.transform.position;  //플레이어가 부쉬에 들어갈 당시의 위치
  87.             navMeshAgent.SetDestination(spotPos);   //플레이어가 부쉬에 들어간 지점의 위치로 목적지 설정.
  88.             navMeshAgent.stoppingDistance = 1;  //부쉬에 들어간 지점과의 거리는 1이 되면 되도록 설정.
  89.            
  90.         }
  91.     }
  92.  
  93.     private void OnEnable()
  94.     {
  95.         Enemy = null;   //초기화???
  96.         EnemyDir = transform.position;  //???
  97.     }
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement