///////////////////////////////////////////// public Enemy SpawnEnemy(GameObject enemyPrefab) { //Instantiate, set position and rotation GameObject enemyGameObject = Object.Instantiate(enemyPrefab); Enemy enemy = enemyGameObject.GetComponent <Enemy>(); if (enemy == null) { Object.Destroy(enemyGameObject); return(null); } //Set position Vector3 position = Vector3.zero; int tries = 0; bool positionUnvailable; enemyGameObject.SetActive(false); do { GetPosition(ref position); position.y = enemy.GetHeight() / 2; enemy.transform.position = position; //Set rotation Vector3 lookatPosition = mPlayerObject.transform.position; lookatPosition.y = enemy.transform.position.y; enemy.transform.LookAt(mPlayerObject.transform.position); tries++; positionUnvailable = PositionChecker.CheckAreaAvailability(enemy.transform.position, enemy.transform.localScale, enemy.transform.rotation, 1 << LayerMask.NameToLayer("Enemy")); } while (positionUnvailable && tries < mMaxTries); //Max attempts reached if (positionUnvailable) { GameObject.Destroy(enemy); return(null); } enemyGameObject.SetActive(true); return(enemy); }
///////////////////////////////////////////// protected bool IsNearPlayer() { return(PositionChecker.CheckAreaAvailability(NextStepPosition(), transform.localScale, transform.rotation, 1 << LayerMask.NameToLayer("Player"))); }