public void Attack() { float distance = Vector3.Distance(target.transform.position, transform.position); Vector3 dir = (target.transform.position - transform.position).normalized; float direction = Vector3.Dot(dir, transform.forward); Debug.Log("Attack distant " + distance); if (distance < 8f) { if (direction > 0) { EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth"); eh.AddjustCurrentHealth(-10); } } }
//атака Enemy private void Attack() { //вводим переменную distance и вычисляем расстояние между игроком //и его целью float distance = Vector3.Distance(target.transform.position, transform.position); //вычисляем единичный векор направления к цели Vector3 dir = (target.transform.position - transform.position).normalized; //вычисляем нахождение цели в поле зрения (значение 0 и отрицательное - сзади) //значение+впереди. Значение меняется от -1 до 1 float direction = Vector3.Dot(dir, transform.forward); //усли дистанция меньше максимальной, то можем нанести урон if (distance < 2 && direction > 0) { //У цели Enemy ищим компонет с именем EnemyHealth - скрипт EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth"); //и используем его метод изменения состояния здоровья //(необходимо переписать AddjustCurrentHeath с учетом новых требований) eh.AddjustCurrentHealth(-10); } }
//Function for attack. private void Attack() { foreach (GameObject target in targets) { //If there is a target damage will be dealt. if (target != null) { //Variable for distance between player and enemy. float distance = Vector3.Distance(target.transform.position, transform.position); //If the distance is below 2, the player will deal damage. if (distance < 2) { //Gets the component for the enemy's health. EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth"); //Enemy looses 10 health. eh.AddjustCurrentHealth(-10); } } } }
// Update is called once per frame void Update() { enemyObjects = GameObject.FindGameObjectsWithTag("Enemy2"); if (enemyObjects.Length != 0) { GameObject constVar = GameObject.FindGameObjectWithTag("Constant"); StoredInformation stored = constVar.GetComponent <StoredInformation>(); int counter = stored.BattlePosition; MagicAttack pl; GameObject[] obj = GameObject.FindGameObjectsWithTag("Player2"); pl = obj[counter].GetComponent <MagicAttack>(); enemyObjects = GameObject.FindGameObjectsWithTag("Enemy2"); BattleAttackDisplay battleAttack = obj[counter].GetComponent <BattleAttackDisplay>(); funTimes = battleAttack.EnemyNumber; EnemyTarget = enemyObjects[funTimes]; if (EnemyTarget == null) { EnemyTarget = enemyObjects[funTimes + 1]; } if (attempt) { EnemyTarget = enemyObjects[funTimes]; EnemyHealth attackEnemy = EnemyTarget.GetComponent <EnemyHealth>(); attackType = battleAttack.attackType; if (attackType == "Fire") { initialiseParticles = Instantiate(particles[0], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Fire"; attackEnemy.AddjustCurrentHealth(stored._manaValue[0], "minus"); } if (attackType == "Wind") { initialiseParticles = Instantiate(particles[1], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Wind"; attackEnemy.AddjustCurrentHealth(stored._manaValue[1], "minus"); } if (attackType == "Ice") { initialiseParticles = Instantiate(particles[2], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Ice"; attackEnemy.AddjustCurrentHealth(stored._manaValue[2], "minus"); } if (attackType == "Water") { attackEnemy.AddjustCurrentHealth(stored._manaValue[3], "minus"); initialiseParticles = Instantiate(particles[3], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Water"; } if (attackType == "Lightning") { initialiseParticles = Instantiate(particles[4], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Lightning"; attackEnemy.AddjustCurrentHealth(stored._manaValue[4], "minus"); } pl.attempt = false; BattleTimeScript batTimeScript = obj[counter].GetComponent <BattleTimeScript>(); batTimeScript.timeToAttack1 = 0; animation.Play("idle"); } } }
// Update is called once per frame void Update() { enemyObjects = GameObject.FindGameObjectsWithTag("Enemy2"); if (enemyObjects.Length != 0) { GameObject constVar = GameObject.FindGameObjectWithTag("Constant"); StoredInformation stored = constVar.GetComponent <StoredInformation>(); int counter = stored.BattlePosition; PlayerAttack playerAttack; GameObject[] obj = GameObject.FindGameObjectsWithTag("Player2"); playerAttack = obj[counter].GetComponent <PlayerAttack>(); enemyObjects = GameObject.FindGameObjectsWithTag("Enemy2"); BattleAttackDisplay bat = obj[counter].GetComponent <BattleAttackDisplay>(); fighterNumber = bat.EnemyNumber; EnemyTarget = enemyObjects[fighterNumber]; if (EnemyTarget == null) { EnemyTarget = enemyObjects[fighterNumber + 1]; } if (characterTransform == null) { characterTransform = transform; } if (attemptAttack) { Quaternion rot; Vector3 lookRotate = EnemyTarget.transform.position - characterTransform.position; rot = Quaternion.Slerp(characterTransform.rotation, Quaternion.LookRotation(lookRotate), 3 * Time.deltaTime); characterTransform.rotation = rot; characterTransform = GameObject.FindGameObjectWithTag("Player2").transform; Vector3 charPosition = new Vector3(characterTransform.forward.x * 8 * Time.deltaTime, 0, characterTransform.forward.z * 8 * Time.deltaTime); characterTransform.position += charPosition; animation.Play("Run"); } if (Vector3.Distance(characterTransform.position, EnemyTarget.transform.position) <= 1) { EnemyTarget = enemyObjects[fighterNumber]; EnemyHealth attackEnemy = EnemyTarget.GetComponent <EnemyHealth>(); attackType = bat.attackType; if (attackType == "Attack") { attackEnemy.AddjustCurrentHealth(stored._attackValue[0], "minus"); } if (attackType == "FireAttack") { initialiseParticles = Instantiate(particles[0], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Fire"; attackEnemy.AddjustCurrentHealth(stored._attackValue[1], "minus"); } if (attackType == "IceAttack") { attackEnemy.AddjustCurrentHealth(stored._attackValue[2], "minus"); initialiseParticles = Instantiate(particles[1], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Ice"; } if (attackType == "LightningAttack") { initialiseParticles = Instantiate(particles[2], EnemyTarget.transform.position, Quaternion.identity) as GameObject; initialiseParticles.name = "Lightning"; attackEnemy.AddjustCurrentHealth(stored._attackValue[3], "minus"); } playerAttack.attemptAttack = false; GameObject[] atemptingChange = GameObject.FindGameObjectsWithTag("PlayerBattle"); obj[counter].transform.position = atemptingChange[counter].transform.position; BattleTimeScript batTimeScript = obj[counter].GetComponent <BattleTimeScript>(); batTimeScript.timeToAttack1 = 0; animation.Play("idle"); } } }