Need help instantiating enemies at a set Viewport Position before my camera enters the room.
Hello!
I'm currently working on a 2D game where I'm trying to pre-populate my level with encounters, I want the enemies already in the room when the player enters. Right now the encounter is triggered upon entering the room when the camera bumps into the collider and the enemies are instantiated at the Viewport position, however because it does it while the camera is moving into the room the position is incorrect. Using hard values isn't an option because the maps could of course change and also I may want them randomly generated at some point.
Is there a way I can I instantiate an enemy at the same point on the screen regardless of resolution in each specific room at the start of the level?
Encounters will have up to 3 enemies and they will always hold the same formation (with spots randomized to add diversity)
Here is my instantiating code:
[SerializeField] public List<GameObject> levelEnemies = new List<GameObject>();
GameObject levelEnemy;
Vector3 enemyPosition;
Vector3 viewportPosition = new Vector3(0.8f, 0.5f, 10f); // Place on the screen I decided I want single enemies to appear
Camera cam;
void Start()
{
Debug.Log("Enemy Triggered");
cam = Camera.main;
enemyPosition = cam.ViewportToWorldPoint(viewportPosition);
levelEnemy = Instantiate(levelEnemies[0], enemyPosition, Quaternion.identity); //instantiate test enemy at the converted position
//navigation.DisableNavigation();
}
Edit: To clarify, when I just move the camera to the rooms without the collider and instantiate manually the enemies appear in the right position in the rooms, this is basically what I want but BEFORE the camera hits the room so the enemies aren't "teleporting" in.
Here is what happens when the camera collider bumps into the encounter collider
Here's what should be happening when the enemy is instantiated (triggered manually through the editor once camera has been moved fully)
Your answer
Follow this Question
Related Questions
Need help with syncing gameObject [UNET] 0 Answers
Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter? 0 Answers
instantiate 1 Answer
Change color of object with C# 1 Answer
Gameobject array 0 Answers