Generating Random Obstacles | MAKING AN INFINITE RUNNER game like CANABALT #5 | Unity Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ธ.ค. 2024
  • Adding a bit of difficulty by spawning obstacles on top of the procedurally generated platforms, and of course, running into them.
    Project Files : ko-fi.com/s/75...
    Support/Follow
    Patreon / superjustin5000
    Coffee www.ko-fi.com/...
    Stream / superjustin5000
    Discord / discord
    www.superjusti...
    I also like
    btc - bc1qv6ktxh9kcf6juxjxhqmyjrg6ptv48ptlqu679x
    eth - 0x6b7708Ec9b120c1CA2239c9452060086f83AD9E8
    doge - DKu8DjKL78THAHBz2eh3U9EGQD7QSNnWgM
    Disclaimer: This video assumes you have some beginner knowledge of the Unity IDE. I am using Unity 2021, Visual Studio 2019, and Windows 10 Professional.
    #gamedev #indiegamedev #gamedevelopment #unity3d #unity3d

ความคิดเห็น • 29

  • @runestmoen6060
    @runestmoen6060 ปีที่แล้ว

    Thanks

  • @riuzakyrodriguez1486
    @riuzakyrodriguez1486 3 ปีที่แล้ว +2

    you saved my homework man, great series

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว +1

      I wish could have had homework like this back in the day 😎

  • @tauheedgamedev2388
    @tauheedgamedev2388 3 ปีที่แล้ว +2

    Wow man, I didn't know you had your own channel separate to Super Game Bros. Subbed and great Tutorial

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว +1

      Hey thanks! That means a lot. Cheers dude

  • @DevNoob
    @DevNoob 3 ปีที่แล้ว

    Think. Code. Drink. What a great order man! I've been wanting to make an infinite runner for a while now...

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว

      Haha, and the order matters! Make it man, it's a fun style of game, because you can take it in so many directions from just the base concept of GO FAST! REACT!

  • @revn45
    @revn45 3 ปีที่แล้ว +1

    Great video!

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว

      Hey man thank you! And thanks for sticking around for the whole series so far.

  • @blurr3409
    @blurr3409 ปีที่แล้ว +2

    I got this bug where I am colliding with the obstacles but some obstacles does not disappear or slowdown my character. I do not know what is causing this bug I triple checked my code I coded it right. If anyone can help it would be great!

    • @oswaldodavidgarciarodrigue2301
      @oswaldodavidgarciarodrigue2301 ปีที่แล้ว +1

      I had the same problem too! It seems is related to the bug of the player landing on an obstacle and falling down. In this video the author fixed the bug I mentioned, and after checking, it also solves the problem we have.
      th-cam.com/video/hqBrukAlB6k/w-d-xo.html

  • @francescobortolamei7653
    @francescobortolamei7653 2 ปีที่แล้ว +2

    I love your tutorials. but i have a little problem to the height of buildings... they height is so similar and this makes the game very easy. ( i don't create obsacles yet)
    Here's my code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Ground : MonoBehaviour
    {
    Player player;

    public float groundHeight;
    public float groundRight;
    public float screenRight;
    BoxCollider2D collider;
    bool didGenerateGround = false;
    private void Awake()
    {
    player = GameObject.Find("Player").GetComponent();
    collider = GetComponent();
    groundHeight = transform.position.y + (collider.size.y / 2);
    screenRight = Camera.main. transform.position.x * 2;
    }
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
    Vector2 pos = transform.position;
    pos.x -= player.velocity.x * Time.fixedDeltaTime;

    groundRight = transform.position.x + (collider.size.x / 2);
    if (groundRight < -22f)
    {
    Destroy(gameObject);
    return;
    }
    if (!didGenerateGround)
    {
    if(groundRight < screenRight)
    {
    didGenerateGround = true;
    generateGround();
    }
    }
    transform.position = pos;
    }
    void generateGround()
    {
    GameObject go = Instantiate(gameObject);
    BoxCollider2D goCollider = go.GetComponent();
    Vector2 pos;
    float h1 = player.jumpVelocity * player.maxHoldJumpTime;
    float t = player.jumpVelocity / -player.gravity;
    float h2 = player.jumpVelocity * t + (0.2f * (player.gravity * (t * t)));
    float maxJumpHeight = h1 + h2;
    float maxY = maxJumpHeight * 0.7f;
    maxY += groundHeight;
    float minY = 9;
    float actualY = Random.Range(minY, maxY);
    pos.y = actualY - goCollider.size.y / 2;
    if (pos.y > 7)
    pos.y = 7;
    float t1 = t + player.maxHoldJumpTime;
    float t2 = Mathf.Sqrt((2.0f * (maxY - actualY))) / -player.gravity;
    float totalTime = t1 + t2;
    float maxX = totalTime * player.velocity.x;
    maxX *= 0.7f;
    maxX += groundRight;
    float minX = screenRight + 5;
    float actualX = Random.Range(minX, maxX);
    pos.x = actualX + goCollider.size.x / 2;
    go.transform.position = pos;
    Ground goGround = go.GetComponent();
    goGround.groundHeight = go.transform.position.y + (goCollider.size.y / 2);
    }
    }
    p.s my canvas is a little smaller than yours and in another position.

    • @superjustin5000
      @superjustin5000  2 ปีที่แล้ว

      I see, that's probably why. You'll have to choose different values. See if you can debug the values of actualY and see if the "if (pos.y > 7)" is coming back as true too often.

    • @francescobortolamei7653
      @francescobortolamei7653 2 ปีที่แล้ว

      ok, thank you very much for the answer ^^

  • @justVisfine
    @justVisfine ปีที่แล้ว

    I'm having an issue where the obstacles are generating at heights generated by the previous platform, causing them to generate either in the platform or above it. any suggestions?

    • @justVisfine
      @justVisfine ปีที่แล้ว

      Here's my code:
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      public class ground : MonoBehaviour
      {
      Player player;
      public float groundHeight;
      public float groundRight;
      public float screenRight;
      BoxCollider2D collider;
      bool didGenerateGround = false;
      public obstacle boxTemplate;
      private void Awake()
      {
      player = GameObject.Find("Player").GetComponent(); ;
      collider = GetComponent();
      groundHeight = transform.position.y + (collider.size.y / 2);
      screenRight = Camera.main.transform.position.x * 2;
      }
      // Start is called before the first frame update
      void Start()
      {

      }
      // Update is called once per frame
      void Update()
      {

      }
      private void FixedUpdate()
      {
      Vector2 pos = transform.position;
      pos.x -= player.velocity.x * Time.fixedDeltaTime;
      groundHeight = transform.position.y + (collider.size.y / 2);
      if (groundRight < (0-30))
      {
      Destroy(gameObject);
      return;
      }
      groundRight = transform.position.x + (collider.size.x / 2);
      if(groundRight < screenRight)
      {
      if(didGenerateGround == false)
      {
      didGenerateGround = true;
      generateGround();
      }
      }
      transform.position = pos;
      }
      void generateGround()
      {
      GameObject go = Instantiate(gameObject);
      BoxCollider2D goCollider = go.GetComponent();
      Vector2 pos;
      float h1 = player.jumpVelocity * player.maxHoldJumpTime;
      float t = player.jumpVelocity / -player.gravity;
      float h2 = player.jumpVelocity * t + (0.5f * (player.gravity * (t * t)));
      float maxJumpHeight = h1 + h2;
      float maxY = maxJumpHeight * 0.4f;
      maxY += groundHeight;
      float minY = 1;
      float actualY = Random.Range(minY, maxY) - goCollider.size.y / 2;
      pos.y = actualY;
      if (pos.y > -4)
      {
      pos.y = -4;
      }
      if (pos.y < -11)
      {
      pos.y = -11;
      }
      float t1 = t + player.maxHoldJumpTime;
      float t2 = Mathf.Sqrt((2.0f * (maxY - actualY)) / (0 - player.gravity));
      float totalTime = t1 + t2;
      float maxX = totalTime * player.velocity.x;
      maxX *= 0.9f;
      maxX += groundRight;
      float minX = screenRight + 50;
      float actualX = Random.Range(minX, maxX) + (goCollider.size.x / 2);
      pos.x = actualX;
      go.transform.position = pos;
      ground goGround = go.GetComponent();
      goGround.groundHeight = transform.position.y + (goCollider.size.y / 2);
      int obstacleNum = Random.Range(0, 4);
      for (int i = 0; i < obstacleNum; i++)
      {
      GameObject Box = Instantiate(boxTemplate.gameObject);
      float y = goGround.groundHeight + 1;
      float halfWidth = (goCollider.size.x / 2) - 1;
      float left = go.transform.position.x - halfWidth;
      float right = go.transform.position.x + halfWidth;
      float x = Random.Range(left, right);
      Vector2 boxPos = new Vector2(x, y);
      Box.transform.position = boxPos;
      }
      }
      }

  • @iloveabg200
    @iloveabg200 2 ปีที่แล้ว

    My code was identical to yours, but way too many boxes where spawned and some were floating. How could i fix this? Thanks.

    • @superjustin5000
      @superjustin5000  2 ปีที่แล้ว

      Did the floating get fixed from your other comment? May be related? Other than that I'd have to look at the project / code to be sure of what's going on

  • @3rd_dimensional590
    @3rd_dimensional590 ปีที่แล้ว

    someone pls help! When it comes to the boxes sometimes my player just runs right through them. I have no idea what causes it and it seems to just happen randomly. Does anyone at least know what I can check or look out for to fix this. thx

    • @superjustin5000
      @superjustin5000  ปีที่แล้ว

      Hey, someone else mentioned the exact same thing in one of the other videos in this series, so here's my reply from that (hope it helps) :
      "
      Hey thanks!
      Funny enough I was just running into a similar issue in my other project, so thanks for bringing it up. The raycast should work similarly to a continuous collider collision check so you can try that too, but I think it's because the position and velocity are incremented before the actual raycast check is happening, so the raycast is a frame off / ahead.
      Try to first save the previous position into a variable before advancing it by velocity and use the previous position when calculating the raycast origin.
      OR.. just do the raycast checking earlier and do the updates of position and velocity later, or at the end of the update or something.
      If that's not entirely the reason this issue is presenting itself, it's at least partly responsible.
      "

    • @3rd_dimensional590
      @3rd_dimensional590 ปีที่แล้ว

      are you talking about this line of code "distance += velocity.x * Time.fixedDeltaTime;" i can just move that after the raycast checks? sorry im not 2 great at coding i dont fully understand how to implement ur solution
      @@superjustin5000

  • @TheSzymonc
    @TheSzymonc 3 ปีที่แล้ว

    Small bug there, when you land on obstacle player falls down (never made contact with ground I guess)

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว +1

      You are correct my friend, if you land just right you can trigger. What I get for going too fast. Thanks for catching it!

    • @cristobalmella7949
      @cristobalmella7949 3 ปีที่แล้ว

      @@superjustin5000 How do you solve that bug?

    • @superjustin5000
      @superjustin5000  3 ปีที่แล้ว +1

      @@cristobalmella7949 This is exactly the reason I released part 7 recently 😎

  • @FF_2wiCe
    @FF_2wiCe 2 ปีที่แล้ว

    😁😍😍😍😍😍

  • @arabiannights7183
    @arabiannights7183 3 ปีที่แล้ว +1

    Don't be stingy ! Give us the script in description link.or else I have no reason to subscribe to your channel. There are 1000s of game dev teachers in youtube

    • @hotlinefrenzy
      @hotlinefrenzy 3 ปีที่แล้ว +3

      Bruh men just go to the project files link in the description

    • @arabiannights7183
      @arabiannights7183 3 ปีที่แล้ว +1

      @@hotlinefrenzy yes I know. He put the project files after I wrote this comment