- Home /
Prefab instantiated off screen problem
Hi everyone. I am working on a 2D shooter game, kind of a spin-off of Space Invaders. The "enemies" are generated on the left/right side of the screen (side being chosen at random). The ones generated on the right are slightly higher than the ones on the left so that they do not collide.
I have a small problem. On smaller screens, all enemies are generated as intended. However; on larger screens, the enemies coming from the right (top) are being generated above the top of the screen therefore never seen by the player. Here's the code I am using.
function GenerateEnemies()
{
var tempEnemy:Rigidbody;
var enemyDirection = Random.Range(1,3);
var posY1 = topMargin-30;
var posY2 = topMargin-60;
if(enemyDirection == 1)
{
tempEnemy = Instantiate(enemy,Vector3(rightMargin,posY1,8),Quaternion.identity);
tempEnemy.GetComponent(EnemyBehaviour).direction = 1;
}
if(enemyDirection == 2)
{
tempEnemy = Instantiate(enemy,Vector3(leftMargin,posY2,8),Quaternion.identity);
tempEnemy.GetComponent(EnemyBehaviour).direction = 2;
tempEnemy.renderer.material = enemyMaterial;
}
}
Thanks for any help.
Matthew
Hey $$anonymous$$atthew! Already working on a new game? :)
How is top$$anonymous$$argin calculated?
Yes, xikky has the right of it, this depends on how your margins are calculated. People need to see more code to find your problem.
This problem was solved by not depending on the top margin, which was calculated by the Screen to world point...
Your answer
![](https://koobas.hobune.stream/wayback/20220613104704im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate bullet toward object 2 Answers
How to tell the number of instantiated object 2 Answers
Trying to load a prefab but getting null error 4 Answers
Creating a GameObject between two 3 Answers
prefab updating wrong.. 1 Answer