Enemies spawning outside of camera view problem

Hey there sorry I am feeling like a total noob with my issue here.
I made a game and the enemies spawn just fine in the resolution I first started it out with 1920 x 1080,
but when I change my aspect ratio to something else such as 4x3 the enemies spawn outside of the desired spawn area.


    private void SpawnEnemy(BottomEnemy myEnemy)
    {
                newPosition = new Vector3(UnityEngine.Random.Range(-9.0f, 9.0f), -5, 0);
              
    }

           Enemy newEnemy =
            Instantiate(myEnemy, newPosition, transform.rotation)
            as Enemy;

I was looking at ScreenToWorldPoint and trying to find a way to convert my Vector3 to stay inside the camera ratio but I cannot figure this out or sure thats the correct route to go, please help.

OK So I figured out how to do it with CameraViewToWorldPoint, this is my actual code before was a simple dummy code of how it worked. I had to reference the camera and stuff then a ton of other errors completely unrelated to my enemies are happening now. :frowning:

I feel like I completely broke my game, the apple had a spin on him thats happening in another script and its spinning way too fast now, and The enemies are spawning really tiny now… I guess I can finess the size of them but I feel like this should not of happened.

Does anyone know whats going on?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BottomEnemySpawnerFire : MonoBehaviour
{
    [SerializeField] BottomEnemy[] EnemyPrefabArray = null;

    public int specialNoteInt;
    public int musicNoteCount;

    //cached references
    GameObject Timer2;
    MyTimer2 myTimer2;

    GameObject Timer;
    MyTimer myTimer;

    Camera camera;


    //to change what enemy pos
    int numEnemy;
    Vector3 newPosition;



    private void Start()
    {

        //cached reference compenent getting?
        Timer2 = GameObject.Find("Timer2");
        myTimer2 = Timer2.GetComponent<MyTimer2>();

        Timer = GameObject.Find("Timer");
        myTimer = Timer.GetComponent<MyTimer>();




        specialNoteInt = 0;
        musicNoteCount = 0;
        numEnemy = 1;


        camera = Camera.main;

    }







    //Music Notes Appear on These Seconds (Consider Using Audacity To Zoom into each second)
    float[] MusicNoteArray = {
        //0 second
        0.05f, 0.25f, 0.35f, 0.45f, 0.65f, 0.75f, 0.85f, 1.05f, 1.15f, 1.25f, 1.45f, 1.55f,   /*other notes*/ 35f, 35.5f,

        46.15f, 46.8f, 47.2f, 47.6f,



      




        //...

        /*make sure you keep a very large number in*/  1000f

    };


    /*incase you want to make certian notes larger or something*/
    int[] specialNote =
    {
        //0,
      


        /*make sure you keep a very large number in*/ 1000
    };





  

    private void SpawnEnemy(BottomEnemy myEnemy)
    {
      
        if (myTimer.timer > .25)
        {
            // Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
            if (numEnemy == 1)
            {
                //newPosition = new Vector3(UnityEngine.Random.Range(-9.0f, 0), -5, 0);
              
                newPosition = camera.ViewportToWorldPoint(new Vector3(UnityEngine.Random.Range(0f, 1),
                    UnityEngine.Random.Range(0, 1), -camera.transform.position.z));
                numEnemy = 0;
            }
            else if (numEnemy == 0)
            {
                //newPosition = new Vector3(UnityEngine.Random.Range(0, 9.0f), -5, 0);
                newPosition = camera.ViewportToWorldPoint(new Vector3(UnityEngine.Random.Range(0f, 1),
                    UnityEngine.Random.Range(0, 1), -camera.transform.position.z));

                numEnemy = 1;
            }




            BottomEnemy newEnemy =
            Instantiate(myEnemy, newPosition, transform.rotation)
            as BottomEnemy;



            newEnemy.name = newEnemy.name + MusicNoteArray[musicNoteCount] + " #(" + musicNoteCount + ")";
            //newEnemy.name = "Enemy" + musicNoteCount;

            newEnemy.transform.parent = transform;


            if (musicNoteCount == specialNote[specialNoteInt])
            {
                newEnemy.transform.localScale = new Vector3(2F, 2F, 0);
                specialNoteInt++;
            }
            else
            {
                newEnemy.transform.localScale = new Vector3(1F, 1F, 0);
            }


        }


    }







    void SpawnNotesOnTime()
    {


        if (musicNoteCount < MusicNoteArray.Length) //do this stuff while
        {




            if (myTimer2.timer >= MusicNoteArray[musicNoteCount])//if the timer has gotten larger than the time to spawn
            {


                SpawnEnemy(EnemyPrefabArray[UnityEngine.Random.Range(0, EnemyPrefabArray.Length)]); ///spawn a random enemy from the array

                musicNoteCount += 1;

                return;
            }
        }
        else
        {
            return;
        }




    }

    private void Update()
    {





        SpawnNotesOnTime();






    }





}

OK Sorry for tripple posting but I guess all I got to do is double to local scale now, it just sucks cause I have to modify this in 20 or more scripts in several different aspects, well I guess this is how you learn :slight_smile:

I always try to keep in mind that there is no rush to finish my game but man I get such lovely headaches. Thanks for bearing with me. Hope this helps anyone in the future, even though I am a beginner.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BottomEnemySpawnerFire : MonoBehaviour
{
    [SerializeField] BottomEnemy[] EnemyPrefabArray = null;

    public int specialNoteInt;
    public int musicNoteCount;

    //cached references
    GameObject Timer2;
    MyTimer2 myTimer2;

    GameObject Timer;
    MyTimer myTimer;

    Camera camera;


    //to change what enemy pos
    int numEnemy;
    Vector3 newPosition;



    private void Start()
    {

        //cached reference compenent getting?
        Timer2 = GameObject.Find("Timer2");
        myTimer2 = Timer2.GetComponent<MyTimer2>();

        Timer = GameObject.Find("Timer");
        myTimer = Timer.GetComponent<MyTimer>();




        specialNoteInt = 0;
        musicNoteCount = 0;
        numEnemy = 1;


        camera = Camera.main;

    }







    //Music Notes Appear on These Seconds (Consider Using Audacity To Zoom into each second)
    float[] MusicNoteArray = {
        //0 second
        0.05f, 0.25f, 0.35f, 0.45f, 0.65f, 0.75f, 0.85f, 1.05f, 1.15f, 1.25f, 1.45f, 1.55f,   /*other notes*/ 35f, 35.5f,

        46.15f, 46.8f, 47.2f, 47.6f,



     




        //...

        /*make sure you keep a very large number in*/  1000f

    };


    /*incase you want to make certian notes larger or something*/
    int[] specialNote =
    {
        //0,
     


        /*make sure you keep a very large number in*/ 1000
    };





 

    private void SpawnEnemy(BottomEnemy myEnemy)
    {
     
        if (myTimer.timer > .25)
        {
            // Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
            if (numEnemy == 1)
            {
                //newPosition = new Vector3(UnityEngine.Random.Range(-9.0f, 0), -5, 0);
             
                newPosition = camera.ViewportToWorldPoint(new Vector3(UnityEngine.Random.Range(0f, 1),
                    UnityEngine.Random.Range(0, 1), -camera.transform.position.z));
                numEnemy = 0;
            }
            else if (numEnemy == 0)
            {
                //newPosition = new Vector3(UnityEngine.Random.Range(0, 9.0f), -5, 0);
                newPosition = camera.ViewportToWorldPoint(new Vector3(UnityEngine.Random.Range(0f, .5f),
                    UnityEngine.Random.Range(0, 1), -camera.transform.position.z));

                numEnemy = 1;
            }




            BottomEnemy newEnemy =
            Instantiate(myEnemy, newPosition, transform.rotation)
            as BottomEnemy;
         


            newEnemy.name = newEnemy.name + MusicNoteArray[musicNoteCount] + " #(" + musicNoteCount + ")";
            //newEnemy.name = "Enemy" + musicNoteCount;

            newEnemy.transform.parent = transform;


            if (musicNoteCount == specialNote[specialNoteInt])
            {
                newEnemy.transform.localScale = new Vector3(4F, 4F, 0);
                specialNoteInt++;
            }
            else
            {
                newEnemy.transform.localScale = new Vector3(2F, 2F, 0);
            }


        }


    }







    void SpawnNotesOnTime()
    {


        if (musicNoteCount < MusicNoteArray.Length) //do this stuff while
        {




            if (myTimer2.timer >= MusicNoteArray[musicNoteCount])//if the timer has gotten larger than the time to spawn
            {


                SpawnEnemy(EnemyPrefabArray[UnityEngine.Random.Range(0, EnemyPrefabArray.Length)]); ///spawn a random enemy from the array

                musicNoteCount += 1;

                return;
            }
        }
        else
        {
            return;
        }




    }

    private void Update()
    {





        SpawnNotesOnTime();






    }





}

My final script for a basic spawn enemy with screentoworldpoint, man I wish there was better tutorials for what you needed when you need them lol

Still not sure why the apple is now spinning at 100x his previous rate or if its even related but I am sure I can figure that out.

Edit
Alright the Spinning out of control issue was resolved by restraining the z on the rigidbody for the mouse guy, I guess it’s unrelated to the issue I had but I just want to let you know I figured everything out, now I just need to go modify everything on 10 different scenes and 25+ scripts :cry: may god be with me.