// part of patrol time // Use this for initialization void Start() { // start with patroll CharacterAI = GetComponent <Rigidbody2D>(); actualState = CharacterBotAction.Patrol; funcPertinence = new float[4]; if (data == null) { data = GetComponent <CharacterData>(); } }
void Update() { if (GameManager.isPaused == false) { GameTime += Time.deltaTime; IAVerification(); if (action == CharacterBotAction.Chase) { Vector2 playerDistance = player.transform.position - gameObject.transform.position; character.velocity = playerDistance * (float)characterData.CharacterVelocity(); } if (action == CharacterBotAction.Idle) { character.velocity = Vector2.zero; // rules to patrol } direction = GetDirection(); SetAnimatedObjectByAction(); } else { direction = 0; action = CharacterBotAction.Idle; AnimationSelectionID = 0; TimeAtack = 0; GameTime = 0; character.velocity = Vector2.zero; } }
// Update is called once per frame void Update() { if (!GameManager.isPaused && ( GameManager.State != GameManager.GameState.youwin && GameManager.State != GameManager.GameState.speaking && GameManager.State != GameManager.GameState.gameover) ) { GameUpdate(); } else if (GameManager.State == GameManager.GameState.youwin)// you win means player wins { actualState = CharacterBotAction.Die; GameUpdate(); } else // in pause the ai dont move { actualState = CharacterBotAction.Idle; CharacterAI.velocity = Vector2.zero; } }
void IAVerification() { // verify if player is near Vector2 playerDistance = player.transform.position - gameObject.transform.position; if (playerDistance.magnitude < ChaseDistance) { playerIsNear = true; if (playerDistance.magnitude < AtackSpaceDistance) { isNearAtack = true; } else { isNearAtack = false; } } else { playerIsNear = false; } // verify if is near atack if (GameTime < DamangeTime + 0.2) { //damange animation } if (!isNearAtack && !playerIsNear)//for now { action = CharacterBotAction.Idle; } else if ((playerIsNear && isNearAtack && action == CharacterBotAction.Atack && (GameTime > TimeAtack + 0.8f))) { action = CharacterBotAction.Chase; } else if (isNearAtack && GameTime > TimeAtack + TimeBetweenAtack) { TimeBetweenAtack = UnityEngine.Random.Range(1f, 2.5f); action = CharacterBotAction.Atack; TimeAtack = Time.time; } else if ((playerIsNear && !isNearAtack)) { action = CharacterBotAction.Chase; } if (characterData.Life() <= 0) { action = CharacterBotAction.Die; Invoke("DestroyMe", 0.5f); } /* * if (player.GetComponent<CharacterControllerMain>().CharacterData.Life() <= 0) * { * action = CharacterBotAction.Win; * * } * * * */ }
// here player just do some action according to you state void UpdateIAControll()//actuador { // do action if (actualState == CharacterBotAction.Patrol) { if (folowPath != null) { folowPath.FolowPath(); } else { Walk(new Vector2(1, 0), 10); } AnimationSelectionID = 2; } else if (actualState == CharacterBotAction.Die) { CharacterAI.velocity = Vector2.zero;// stop ai Invoke("DestroyMe", 2); } else if (actualState == CharacterBotAction.Chase) { GameObject nearestPlayer; GetPlayerDistance(out nearestPlayer); Vector2 playerDistance = nearestPlayer.transform.position - gameObject.transform.position; CharacterAI.velocity = playerDistance * (float)data.CharacterVelocity(); AnimationSelectionID = 2; } else if (actualState == CharacterBotAction.Atack) { // move to player GameObject nearestPlayer; GetPlayerDistance(out nearestPlayer); Vector2 playerDistance = nearestPlayer.transform.position - gameObject.transform.position; CharacterAI.velocity = playerDistance * (float)data.CharacterVelocity(); // noved to plaer AnimationSelectionID = 3; // this hold a atack for time of MaxDurationOfatack second if (Time.time > UltimoAtack + 0.0f && Time.time < UltimoAtack + MaxdurationOfAtack) // time betwen 0 and maxDurationOfAtack second after atack { actualState = CharacterBotAction.Atack; AnimationSelectionID = 2; } // this give a time betwen last atack and next atack else if (Time.time >= UltimoAtack + MaxdurationOfAtack && Time.time < UltimoAtack + timeBetweenAtacks + MaxdurationOfAtack)// time between 1 and 1.5 after atack { actualState = CharacterBotAction.Chase; } else if (Time.time >= UltimoAtack + timeBetweenAtacks + MaxdurationOfAtack)// time after 1.5 after atack { UltimoAtack = Time.time; actualState = CharacterBotAction.Chase; } } else if (actualState == CharacterBotAction.Flee) { if (Time.time > RandomTime + 1) {// generate random direction to flee between > 1 second RandomTime = Time.time; float x = Random.Range(-5.0f, 5.0f); float y = Random.Range(-5.0f, 5.0f); RandomDirection = new Vector2(x, y); } CharacterAI.velocity = RandomDirection * (float)data.CharacterVelocity(); AnimationSelectionID = 2; } else if (actualState == CharacterBotAction.Idle) { AnimationSelectionID = 0; CharacterAI.velocity = Vector2.zero;// stop ai } }
void GameUpdate() { // update values GameObject nearestPlayer = GameObject.FindGameObjectWithTag("Player"); PlayerDistance = GetPlayerDistance(out nearestPlayer); direction = GetDirection(); SetAnimatedObjectByAction(); //Fuzzy logic Update funcPertinence[0] = NearPlayer(GetPlayerDistance(out nearestPlayer)); funcPertinence[1] = UltraNearPlayer(GetPlayerDistance(out nearestPlayer)); funcPertinence[2] = LessLife(data.Life()); funcPertinence[3] = FarPlayer(GetPlayerDistance(out nearestPlayer)); // valoresRepresentativos [0] entende-se por Jogador perto // valoresRepresentativos [1] entende-se por jogador muito perto // valoresRepresentativos [2] entende-se por pouca vida // valoresRepresentativos [3] entende-se por jogador longe /// get the bigger value to chose a action int idValorRepresentativo = 0; float valorMaior = funcPertinence[0]; for (int conter = 0; conter < funcPertinence.Length; conter++) { if (funcPertinence[conter] > valorMaior) { idValorRepresentativo = conter; // get the id of bigger value valorMaior = funcPertinence[conter]; // get bigger value } } // change action if (actualState == CharacterBotAction.Patrol) { if (idValorRepresentativo == 0 /* jogadores perto */) { actualState = CharacterBotAction.Chase; } else if (idValorRepresentativo == 3 /* jogadores longe */) { actualState = CharacterBotAction.Patrol; } //else if (idValorRepresentativo == 2/* jogadores longe */ ) { actualState = CharacterBotAction.Flee; } } else if (actualState == CharacterBotAction.Die) { if (idValorRepresentativo == 1 /* jogadores mto perto */) { actualState = CharacterBotAction.Atack; } } else if (actualState == CharacterBotAction.Chase) { if (idValorRepresentativo == 0 /* jogadores perto */) { actualState = CharacterBotAction.Chase; } else if (idValorRepresentativo == 1 /* jogadores mto perto */) { actualState = CharacterBotAction.Atack; } else if (idValorRepresentativo == 2 /* eu pouca vida */) { actualState = CharacterBotAction.Flee; } else if (idValorRepresentativo == 3 /* jogadores longe */) { actualState = CharacterBotAction.Patrol; } } else if (actualState == CharacterBotAction.Flee) { if (idValorRepresentativo == 2 /* pouca vida */) { actualState = CharacterBotAction.Flee; } else if (idValorRepresentativo == 1 /* jogadores mto perto */) { actualState = CharacterBotAction.Atack; } // else if (idValorRepresentativo == 0/* jogadores perto */) { actualState = CharacterBotAction.Chase; } } else if (actualState == CharacterBotAction.Atack) { // se ultimo ataque entre 0 segundos e um após inicio do ataque mantem ataque // se entre 1 e dois ataques bloquear ataque // se ultimo ataque mais 2 segundos então liberar ataque if (idValorRepresentativo == 0 /* jogadores perto */) { actualState = CharacterBotAction.Chase; } else if (idValorRepresentativo == 2 /* pouca vida */) { actualState = CharacterBotAction.Flee; } else if (idValorRepresentativo == 1 /* jogadores mto perto */) // atack situation { } } else if (actualState == CharacterBotAction.Idle) { if (idValorRepresentativo == 0 /* jogadores perto */) { actualState = CharacterBotAction.Chase; } else if (idValorRepresentativo == 1 /* jogadores mto perto */) { actualState = CharacterBotAction.Atack; } else if (idValorRepresentativo == 2 /* pouca vida */) { actualState = CharacterBotAction.Flee; } else if (idValorRepresentativo == 3 /* jogadores longe */) { actualState = CharacterBotAction.Idle; } } if (actualState != CharacterBotAction.Atack) { UltimoAtack = 0; } float Life = data.Life(); /// is Dead is the last bescause dead is default bool isDead = (Life <= 0); if (isDead) { actualState = CharacterBotAction.Die; } // do action UpdateIAControll(); }