Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Script logic for obstacles

Discussion in 'Getting Started' started by TheOnlyTruth, Jul 28, 2018.

  1. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Goood morning good people(And Hello JoeStrout if you're reading this, i know you are :D)

    I was having a blast with unity, learning C# on visual basics, understanding lots of stuff and simply upgrading my old code.

    I have decided to create a pretty simple game which would involve avoiding constant barrage of obstacles. However, i have been trying to get my head around on how to actually write code for the obstacles and it seems like its out of my grasp so far. :|

    The player is simply standing on the ground and can move left/right. Obstacles are "falling" onto him in rows - a simple block. I want to make sure that the player always has a way to escape and that hes never trapped in between obstacles.

    Maybe some of you have previous experience on how to go around this?
     

    Attached Files:

  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Hmm. Tricky.

    The problem doesn't seem completely well-defined to me. I mean, the diagram you labeled "good" still traps the player — the best he can do is to go all the way to the left, but then a block still falls on him. He has no way to avoid it. And there is a point in the middle where, at some point while the blocks fall, he will be surrounded, just as in the "bad" case.

    So, you could try to better define what you mean by all this, and then write some code to only generate obstacles that fall into the "good" category.

    Or you could just ignore the problem, and get on with your game. That's what I'd do. :) Tweak how dense these obstacles are by adjusting how often they spawn and how tall they are, and adjust it until it's challenging, but usually possible to survive. And then maybe you'll have an idea for a power-up that lets the player occasionally blast their way through, so that collecting and hoarding these power-ups until they're needed becomes part of the fun — and coincidentally saves you from having to solve the harder problem!

    Or, another way these things are often handled is: don't make the blocks completely random. Instead define valid rows of blocks ahead of time, and just pick from those valid rows whenever you need to spawn a new row of blocks. As long as it's a large set, the player won't notice that they're not random for quite a while.
     
    TheOnlyTruth likes this.
  3. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Thanks! : ) Glad to see you are still so active on the forums!

    Yeah, i guess ill just play around density and adjust maximum distance a player can travel. And make rng spanwner with some adjustments to maximum obstacles that can spawn in a row - so that the max distance is somewhat just a tiny bit more than the maximum distance player can travel from the second of noticing he needs to take action.

    Welp. I guess its an ok solution. >:
     
    JoeStrout likes this.