Search Unity

Question Question about pipe spawning (Flappy Game)

Discussion in '2D' started by NWOL, Dec 28, 2022.

  1. NWOL

    NWOL

    Joined:
    Dec 28, 2022
    Posts:
    1
    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:

    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PipeSpawner : MonoBehaviour
    6. {
    7.     public GameObject pipe;
    8.     public float spawnRate = 2;
    9.     private float timer = 0;
    10.     public float heightOffSet = 4;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         spawnPipe();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (timer < spawnRate)
    21.         {
    22.             timer += Time.deltaTime;
    23.         } else
    24.         {
    25.             spawnPipe();
    26.             timer = 0;
    27.         }
    28.     }
    29.     void spawnPipe()
    30.     {
    31.         float lowestPoint = transform.position.y - heightOffSet;
    32.         float highestPoint = transform.position.y + heightOffSet;
    33.         Instantiate(pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, highestPoint), 0), transform.rotation);
    34.     }
    35. }
    Pipe Movement Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PipeMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5;
    8.     public float deadZone = -40;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
    19.         if (transform.position.x < deadZone)
    20.         {
    21.             Debug.Log("Pipe Destroyed!");
    22.             Destroy(gameObject);
    23.         }
    24.     }
    25.  
    26. }
    27.  
    If im not explaining well let me know.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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!
     
  3. EraCyper

    EraCyper

    Joined:
    Dec 30, 2020
    Posts:
    1
    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

    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 :)
     

    Attached Files:

  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    you should do it like this:

    Code (CSharp):
    1.  
    2.  
    3. float lowestPoint = Camera.ScreenToWorldPoint(new Vector3(0, 0, Camera.nearClipPlane)).y - heightOffSet;
    4.        
    5. float highestPoint = Camera.ScreenToWorldPoint(new Vector3(0, 0, Camera.nearClipPlane)).y + heightOffSet;
    6.    
    for pipes on top of the screen like this:

    Code (CSharp):
    1.  
    2.  
    3. float lowestPoint = Camera.ScreenToWorldPoint(new Vector3(0, Screen.height, Camera.nearClipPlane)).y - heightOffSet;
    4.        
    5. float highestPoint = Camera.ScreenToWorldPoint(new Vector3(0, Screen.height, Camera.nearClipPlane)).y + heightOffSet;
    6.    
     
  5. mrvictordiaz

    mrvictordiaz

    Joined:
    Aug 21, 2016
    Posts:
    12
    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.