How do I instantiate gameobjects vertically?

Hey,

I am working on a 3D game and I am trying to work out a system where there is a gameobject that falls vertically straight down. The user is able to move that gameobject side to side to try and avoid enemies that are spawning beneath him. I have been able to get the side to side movement down and the falling of the gameobject, the only problem that I am running into is trying to instantiate prefabs vertically (the y-axis) instead of horizontally (the x-axis). Here is the code that I have to try and get the objects to spawn.

public GameObject thePlatform;
public GameObject[] theplatforms;

public Transform generationPoint;

public float distanceBetween;

private float platformWidth;
private int platformSelector;

// Start is called before the first frame update
void Start()
{
    platformWidth = thePlatform.GetComponent<BoxCollider>().size.y;
}

// Update is called once per frame
void Update()
{
    if (transform.position.y < generationPoint.position.y)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + platformWidth + distanceBetween);
        platformSelector = Random.Range(0, theplatforms.Length);
        Instantiate(theplatforms[platformSelector], transform.position, transform.rotation);
    }
}  

In the scene, I have it set up where on the main camera there is a gameobject attached and once that object passes through another object is instantiates another object. The current behavior I am getting is when the camera passes through that point the prefab gets instantiated along the x-axis and not the y. If anybody has any suggestions for what I could do that would be great!

Thanks

So, the camera is facing up and you want an object to spawn above it, right? Maybe you can post a screenshot so I can visualize it…

You could use the camera’s position as a base and offset the instantiation from there:

        Instantiate(prefab, new Vector3(camera1.transform.position.x, (camera1.transform.position.y + 5f), camera1.transform.position.z), Quaternion.identity);

I feel like a broken record but please try to not Instantiate (at least objects you intend to destroy to get rid of) objects in the main game loop.


Regardless your position parameter in the Instantiate method is a Vector3 which comes in three constituent parts an x, y, and z field which map to left/right, up/down and forwards and backwards … in relation to the world or to the object at least.


All you need to do is create a new Vector3 where the Y value is set to the value you want.

So try something like:


 public float InitialYPos = 12.0f; // This is a class field
 public GameObject thePlatform;
 public GameObject[] theplatforms;
 public Transform generationPoint;
 public float distanceBetween;
 private float platformWidth;
 private int platformSelector;
 

 // Start is called before the first frame update
 void Start()
 {
     platformWidth = thePlatform.GetComponent<BoxCollider>().size.y;
 }
 // Update is called once per frame
 void Update()
 {
     if (transform.position.y < generationPoint.position.y)
     {
    platformSelector = Random.Range(0, theplatforms.Length);
    Instantiate(theplatforms[platformSelector], new Vector3(transform.position.x, InitialYPos, transform.position.z + platformWidth + distanceBetween), transform.rotation);
     }
 }