0
\$\begingroup\$

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. So I guess ultimately the problem is the camera not being in the right position when they instantiate and not wanting enemies to "pop" in once it is.

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();
}

Here is what my camera collider looks like (Room 1) and my enemy encounter collider looks like (Room 3)

Here is what my camera collider looks like (Room 1) and my enemy encounter collider looks like (Room 3)

Here's what should be happening when the enemy is instantiated (triggered manually through the editor once camera has been moved fully)

Here's what should be happening when the enemy is instantiated (triggered manually through the editor once camera has been moved fully)

Here is what happens when the camera collider bumps into the encounter collider

Here is what happens when the camera collider bumps into the encounter collider

Any help in the right direction would be much appreciated or a workaround!

Here is what my camera collider looks like (Room 1) and my enemy encounter collider looks like (Room 3)

\$\endgroup\$
4
  • \$\begingroup\$ Can you show us a screenshot of your game at the moment you want the enemies to spawn, and a screenshot after the camera has fully entered the room, marking out the viewport positions of the spawn locations in each case? \$\endgroup\$ – DMGregory May 16 '20 at 0:08
  • \$\begingroup\$ Hey DMGregory, added some visuals! \$\endgroup\$ – Tristan Barona May 16 '20 at 0:43
  • \$\begingroup\$ This looks like it could be solved just by spawning at a set point in the room, rather than a set point in the viewport. Can you clarify why spawning relative to the viewport is desirable here? \$\endgroup\$ – DMGregory May 16 '20 at 0:54
  • \$\begingroup\$ The reason I wanted to do this is ultimately because I thought it was how you ensure on larger / smaller screens that the positioning of the enemy will remain the same across screens devices. Also in case I want to add randomly generated rooms down the line, I don't want hard coded coordinates. I just started with Unity late last year and this is my first solo project so if there is a better way to do this dynamically I'm definitely happy to do it another way. \$\endgroup\$ – Tristan Barona May 16 '20 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.