Question about pipe spawning (Flappy Game)

So, im trying to do a game that is just another version of flappy bird (im starting to code now), and my pipes spawn in this way:

8691102--1172604--upload_2022-12-28_19-17-40.png

I wanted them to spawn on the bottom line of the camera, but that doesn’t happen, how could I do that?
Spawn Pipe Code:

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

public class PipeSpawner : MonoBehaviour
{
    public GameObject pipe;
    public float spawnRate = 2;
    private float timer = 0;
    public float heightOffSet = 4;
    // Start is called before the first frame update
    void Start()
    {
        spawnPipe();
    }

    // Update is called once per frame
    void Update()
    {
        if (timer < spawnRate)
        {
            timer += Time.deltaTime;
        } else
        {
            spawnPipe();
            timer = 0;
        }
    }
    void spawnPipe()
    {
        float lowestPoint = transform.position.y - heightOffSet;
        float highestPoint = transform.position.y + heightOffSet;
        Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    }
}

Pipe Movement Code:

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

public class PipeMovement : MonoBehaviour
{
    public float moveSpeed = 5;
    public float deadZone = -40;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
        if (transform.position.x < deadZone)
        {
            Debug.Log("Pipe Destroyed!");
            Destroy(gameObject);
        }
    }

}

If im not explaining well let me know.

8691102--1172604--upload_2022-12-28_19-17-40.png

The code you have spawns pipes at a random vertical position within plus or minus the heightOffset.

This object you are spawning may also have an offset based on how it was made, either through the prefab or through the “hotspot” of the sprite.

If you don’t understand this then you may not be prepared to reason about how to change it.

In that case I suggest you go back to where you got this and finish Step #2 of the two-step tutorial and example code process below.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Hey NWOL!

I’m following the same tutorial from Game Maker’s Toolkit and faced the same issue as you.

I’ve found a simple solution (not sure if it’s an effective run in the long run), but you should take a look at the Inspector in your Unity game.

Once you’ve set in the code

public float heightOffSet = 4;

The PipeSpawner Script automatically fixes the heightOffSet to be at 4, making your lowestPoint and highestPoint to either make your entire go off the game screen, or make the entire pipe show on the game screen (as the issue has been shown in your case).

One way I solved it, was to change the value of the heightOffSet in the Unity Inspector to be lower

8995273--1238812--upload_2023-5-6_9-40-55.png

This is to ensure that the lowestPoint and highestPoint will not cause the entire pipe to show on screen, but instead, only at least up to 80-90% of the pipe shows on the screen.

You can play around the value of the heightOffSet in the Inspector to find what value of heightOffSet works best for you so that your pipe doesn’t show up entirely on screen!

Hope this method helps you :slight_smile:

8995273--1238809--upload_2023-5-6_9-40-27.png

you should do it like this:

float lowestPoint = Camera.ScreenToWorldPoint(new Vector3(0, 0, Camera.nearClipPlane)).y - heightOffSet;
       
float highestPoint = Camera.ScreenToWorldPoint(new Vector3(0, 0, Camera.nearClipPlane)).y + heightOffSet;

for pipes on top of the screen like this:

float lowestPoint = Camera.ScreenToWorldPoint(new Vector3(0, Screen.height, Camera.nearClipPlane)).y - heightOffSet;
       
float highestPoint = Camera.ScreenToWorldPoint(new Vector3(0, Screen.height, Camera.nearClipPlane)).y + heightOffSet;

Basically you want to make sure your sprite origin points are correctly set, and then do the height calculation from there. You can then randomize the Y positions within these boundaries.