Script on enemies:

public class enemy : MonoBehaviour { private Vector3 Player; private Vector2 PlayerDirection; public float Xdif; public float Ydif; private float speed; private float distance; private bool stun; private float stuntime; GameObject playerObj; private Rigidbody2D myRigidbody2D; private bool isEnemyFear; public randomizer fear; // Use this for initialization void Start () { stuntime = 0; stun = false; speed = 6; playerObj = GameObject.Find ("Player"); myRigidbody2D = GetComponent<Rigidbody2D>(); } // Update is called once per frame public void Update () { distance = Vector2.Distance (Player, transform.position); Player = playerObj.transform.position; if (stuntime > 0) { stuntime -=Time.deltaTime; } else { stun = false; } if(fear.CheckFear()){ EnemyFear(); } else{ EnemyAttack(); } } void EnemyAttack(){ if (distance < 25 & !stun) { Xdif = Player.x - transform.position.x; Ydif = Player.y - transform.position.y; PlayerDirection = new Vector2 (Xdif, Ydif); myRigidbody2D.AddForce (PlayerDirection.normalized * speed); Debug.Log("нападают"); } } void EnemyFear(){ if (distance < 25 & !stun) { Xdif = Player.x + transform.position.x; Ydif = Player.y + transform.position.y; PlayerDirection = new Vector2 (Xdif, Ydif); myRigidbody2D.AddForce (PlayerDirection.normalized * speed); Debug.Log("убегают"); } StartCoroutine(Timer()); } void OnCollisionEnter2D(Collision2D Playerhit){ if (Playerhit.gameObject.CompareTag("Player")) { stun = true; stuntime = 1; } } IEnumerator Timer() { yield return new WaitForSeconds(10); isEnemyFear = false; StopCoroutine ("Timer"); } } 

Script on the button:

  public class randomizer : MonoBehaviour { private bool isEnemyFear; public void CheckFear(){ int rand = Random.Range(0, 100); if(rand > 35) isEnemyFear = true; } } 

The CheckFear() function hangs on the button. The script itself is in the so-called manager, not on enemies, not on the player. Now distance = Vector2.Distance (Player, transform.position); and Xdif = Player.x - transform.position.x; Ydif = Player.y - transform.position.y; Xdif = Player.x - transform.position.x; Ydif = Player.y - transform.position.y; can not be calculated correctly because transform.position is taken by the manager. How to isEnemyFear = true to a script on enemies? Since this is done now does not work.

  • And the enemy in the game you have one or they are suitable in waves? If the waves have to implement busting with the closest ones and run your check for everyone. Algorithms for selecting near objects are a whole sea, you need to choose a task that suits you most. - KingPeas
  • the enemy is not alone, the game is something like an isometric RPG, i.e. the player walks around the map and stumbles upon enemies. Changed the script and question. Can you suggest something on this occasion? - MikhailChu

1 answer 1

finally solved this question, here's the code

Script on enemies:

 public class enemy : MonoBehaviour { private Vector3 Player; private Vector2 PlayerDirection; public float Xdif; public float Ydif; private float speed; private float distance; private bool stun; private float stuntime; GameObject playerObj; private Rigidbody2D myRigidbody2D; //private bool isEnemyFear; public randomizer fear; // Use this for initialization void Start () { stuntime = 0; stun = false; speed = 6; playerObj = GameObject.Find ("Player"); myRigidbody2D = GetComponent<Rigidbody2D>(); } // Update is called once per frame public void Update () { distance = Vector2.Distance (Player, transform.position); Player = playerObj.transform.position; if (stuntime > 0) { stuntime -=Time.deltaTime; } else { stun = false; } if(fear.isEnemyFear){ EnemyFear(); } else{ EnemyAttack(); } } void EnemyAttack(){ if (distance < 25 & !stun) { Xdif = Player.x - transform.position.x; Ydif = Player.y - transform.position.y; PlayerDirection = new Vector2 (Xdif, Ydif); myRigidbody2D.AddForce (PlayerDirection.normalized * speed); Debug.Log("нападают"); } } void EnemyFear(){ if (distance < 25 & !stun) { Xdif = Player.x + transform.position.x; Ydif = Player.y + transform.position.y; PlayerDirection = new Vector2 (Xdif, Ydif); myRigidbody2D.AddForce (PlayerDirection.normalized * speed); //AudioController.Play( "zombie" ); Debug.Log("убегают"); } StartCoroutine(Timer()); } void OnCollisionEnter2D(Collision2D Playerhit){ if (Playerhit.gameObject.CompareTag("Player")) { stun = true; stuntime = 1; } } IEnumerator Timer() { yield return new WaitForSeconds(10); fear.isEnemyFear = false; StopCoroutine ("Timer"); } } 

Script on the button:

 public class randomizer : MonoBehaviour { public bool isEnemyFear = false; public void CheckFear(){ int rand = Random.Range(0, 100); if(rand > 35) isEnemyFear = true; } } 

Please comment on whether such a decision is adequate. And is it slow?