Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best Way To Spawn Ground?

Discussion in '2D' started by Dogg, Jun 7, 2014.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I have a question, what's the best way to spawn the ground that your player is going to be walking on? It's just one ground texture but I want it to spawn infinity like an endless runner. I already have it spawning endlessly, but it's not going to work for my game, because I have a powerup that speeds me up, and when I grab it and speed up I go too fast for the ground to spawn and it either kills me, or has a hole in between the grounds spawning. I have a picture that will show you my ground spawns(It's located at the bottom of this post).

    If you saw the picture you will see that I have 4 quads that spawn my ground, and as I already explained it is not the best way, nor is it a smart way. So, what is the best way in your opinions to make the ground spawn without it messing up when I speed up? Thanks in advance.

    Oh and I also have a picture at the bottom that shows how my ground looks. It's not mine I just got it from the Internet right now just to show you an example.
     

    Attached Files:

    Last edited: Jun 8, 2014
  2. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    Can I ask is your ground going to be a flat surface or will it be higher up at certain points in your game?
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Of course you can.:) It's going to be nothing but a flat surface.
     
  4. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    What I would suggest from what I've done myself in my project - make your ground just a bit bigger than what you can see on screen, then apply a script to it to move with your player. I'm not sure if this is a 'good' technique but it works and doesn't mean you're spawning in more than you need to.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    It is a good technique. It's basic and limited, but sometimes those techniques really are the best solutions (and in his scenario, it seems like it is).
     
    CoffeeConundrum likes this.
  6. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Is it supposed to actually move across the screen with the player, or is it supposed to spawn in front of the player forever? Because I attached the ground object onto the main camera and now it follows the player. It works well but the ground looks very stiff and doesn't look like the player is moving at all.
     
  7. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    If he moves the ground with the player wouldn't it seem like the player is running in one place though?Or maybe I got the picture wrong.

    One other idea for spawning could be is to use a collection to store a N number of instances of your ground.Then place each instance next to each other from little before the screen width to the end of it and little further.Now what you have to do is each frame check the position of the first ground instance and compare it with some screen offset.So if the instance is beyond that offset move that instance next to the last instance position.You can imagine it like for example: When the first ground piece is at screen position X, move it's position to the most far piece on the screen.

    This way you won't instantiate the game object, and get some kind of performance overhead.Depending on the speed you will have to see how much instances you will need from the ground to catch up.
     
  8. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    I always worry that the techniques I come up with and use are sometimes not the best so I appreciate that comment :)
     
  9. CoffeeConundrum

    CoffeeConundrum

    Joined:
    Dec 30, 2013
    Posts:
    46
    That's pretty much what you're doing! Best way to resolve that illusion of your player running on the spot would be to use a background image that loops around - you could use two of the same background and when one goes out of view, the other one would start and loop the background like this. Not sure on specifics for code but I'm sure a simple Google search would help you - came across it myself when I was doing some research. Hope this helps :)
     
  10. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I actually do have a background image that loops in the background. Although it's speed is set to 0.0001, so it's kinds slow. The reason why I did that is because it looks better to me. Anyways there are two main things in game that are moving or look like their moving. There is the background, and the obstacles on the ground that spawn(their purpose is to get the player to hit them so that he dies). There is also power ups that also look like they're moving.

    Now when I use your method, it looks like the ground stays completely still while the obstacles are moving toward the player, and the player is running in place trying to dodge the obstacles. When I use my old method, it looks like the player is running forward thanks to the ground moving backwards(The ground doesn't actually move backwards, it's just the illusion of it), and the obstacles go to the player because he's moving forward. That's the way I want it. I wish I could show you my game but It should be kept a secret until release time.
     
  11. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I'm not sure I understand your method, so let me try to understand. Do you want me to use only two ground spawns, and have one on screen at the start of the game, and the other one spawning right at the end of the screen?
     
  12. sysameca

    sysameca

    Joined:
    Mar 2, 2013
    Posts:
    99
    Basically the idea is not so simple for beginner programmer.I can suggest you to begin on something easy, but since you got yourself into this maybe you can read this : http://catlikecoding.com/unity/tutorials/runner/ . The guys almost uses the same logic as i though, but he handles it a little bit different.
     
  13. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Another illusion that I can make is trees or some type of obstacles and put them in the front of the ground to appear on the bottom of the screen(when you play the game), but I do not like that look for my game, any other solutions that you may have?
     
  14. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Yeah it's not simple, but I can maybe do it.
     
  15. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    If I'm reading this thread correctly, it sounds like you want your ground to just be a solid, flat ground forever, correct? No holes or variation or anything)... And just appear to move constantly with the player, like the background you mentioned, but you can stand on it?

    Then just do exactly what you're doing with the background, but with the ground. Instead of thinking of the ground as something special, think of it as a background object. Just one that is shown in the foreground :)
     
  16. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Will the colliders still work if I do that? Also, would this script do the job?

    Code (CSharp):
    1. public class BackgroundScroller : MonoBehaviour {
    2.  
    3.     public float speed = 0;
    4.     public static BackgroundScroller current;
    5.  
    6.     float pos = 0;
    7.  
    8.     void Start(){
    9.         current = this;
    10.     }
    11.  
    12.     void Update(){
    13.         pos += speed;
    14.         if(pos > 1.0f)
    15.             pos -= 1.0f;
    16.         renderer.material.mainTextureOffset = new Vector2(pos, 0);
    17.     }
    18. }
    19.  
     
  17. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    I don't see why the collider wouldn't work if it's still set up the same. Even if for some reason you can't get it to work, you can always make a separate collider object and just put it where you need it (just have no renderer or put it behind the ground or something)
     
  18. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay, but what about the script? I tested the script that I use for the background image, it does not work. I tried editing it but it still doesn't work. Do you have a script that I can use, or a reference script.
     
  19. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    The background's script and settings should be your reference. What differences are there between the background and the ground? That will be the answer to why it's not working. I don't know what part of it isn't working nor do I know how your background works exactly, so I'm not sure how to help you with that. With the script you pasted, it looks like pos never changes and just is always 0, so the if condition always evaluates to false.

    I achieved a similar thing in a different way. This is a completely different solution, but it's from a project of mine. This is for the background, but it would work perfectly fine with an object with a collider on the ground. The object is bigger than the screen and essentially spawns a clone of itself once its x position gets to a certain point and then destroys itself once it gets off the screen. So there's only ever 3 instantiated at once at the most. It's a bit crude, but:

    Code (CSharp):
    1. if(transform.position.x - theCamera.transform.position.x <= -3 && !formedBabby)
    2.         {
    3.             GameObject babby = Instantiate (selfie, new Vector3 (transform.position.x + xOffset, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
    4.             babby.transform.parent = theCamera.transform;
    5.             formedBabby = true;
    6.         }
    7.         if(transform.position.x - theCamera.transform.position.x <= -39)
    8.         {
    9.             Destroy (this.gameObject);
    10.         }
    11.  
    This is the part that does it, pretty much. The formedBabby bool just prevents it from forming over and over. selfie is a reference to what object it needs to spawn -- normally itself, but depending on circumstances it can change in my project, so I went with this. This is probably not the best solution, but it works.

    The numbers were ones I calculated based on the camera size and the object size and stuff. Probably should have put them in variables, but like I said, I'm also a noob and this was my first attempt.

    Code (CSharp):
    1. rigidbody2D.velocity = new Vector2 ((pcs.flySpeed/speedDivider) * -1 , 0);
    This line makes it move relative to the player.
     
  20. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Do I need a separate camera? Because my background has a separate camera, so I probably need another one for the ground.
     
  21. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    I don't think you necessarily would need a separate camera depending on how it was set up. As long as it's a parent of the main camera and the texture shifts regularly or relative to the player or whatever, it should work.

    You could always put it under the same camera as the background, I guess. Just make sure its z position places it in front of the other background elements. You'd have to create some kind of invisible object (you could just use an empty game object and put a collider on it) for you player to land on, so it appears the player is landing on ground.

    I don't see why it shouldn't work on your main camera, though.
     
  22. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay, I'll try that then. Thanks!
     
  23. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Okay so I just finished trying it, and it works pretty well. Although I don't think this technique will work for me. There are multiple reasons to why I think this. First, my obstacles are positioned next to my player, unlike the background and the ground which is now in the back of the player(or so it looks like it). So where my ground used to be, is where my obstacles are except a little farther apart. Moving the obstacles to the back might also not work for me. I don't know it's probably because of the way my script for the obstacles is made.

    The second reason is that for some weird reason the ground turns darker when I have it as a texture, but when it's a sprite it's normal. This makes the ground look like it's night time, even though it's day. I tried using a directional light, but that only made it really bright and it stood out from the rest of the textures/sprites. The reason why I set my ground as a texture, is because the script that I have only works for textures, not sprites.

    The third and final reason is because I don't know how to increase the speed of the scrolling ground to go faster when the player speeds up.
     
  24. shrivastavapranjal10

    shrivastavapranjal10

    Joined:
    Sep 21, 2018
    Posts:
    1
    What will Happen if the player jumps , the ground will follow it and it would be wrong