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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I need help with my first game.

Discussion in '2D' started by andrewlewis379, Jul 31, 2019.

  1. andrewlewis379

    andrewlewis379

    Joined:
    Oct 28, 2018
    Posts:
    10
    Hi there, i wanted to create my first little game, and i need a bit of help with a certain concept.

    So basically it's going to look like this: https://i.imgur.com/YdWTrSe.jpg
    The player can move left/right/up/down and collides with the edges and these barrels but those don't do any harm, they're more like obstacles.

    Now what i need help with is the following: Every 10 seconds i want to spawn an "enemy" orb. They should be bouncing off the edges and barrels, so it needs some sort of constant velocity or force to keep them moving at the same speed, but they need to bounce off the colliders. The player has to dodge those orbs. To prevent it from being impossible after a certain time, i want those orbs to dissapear (destroy themselves) after like 1 minute, so there's only a few orbs around once they started spawning every 10 seconds. and the map doesn't get filled with those orbs. If the player gets hit, he dies and has to restart.

    Basically this spawn system:

    10 seconds after start = 1 enemy orbs
    20 seconds = 2 enemy orbs
    30 seconds = 3 enemy orbs
    40 seconds = 4 enemy orbs
    50 seconds = 5 enemy orbs
    60 seconds = 6 enemy orbs + The first orb destroys itself, so there should now be 5 orbs bouncing around.

    As for those orbs' physics i would also need an explanation of how to keep them at the same speed and keep them bouncing off the colliders.


    This should keep going endlessly, until the player dies.

    I hope someone could help me achieve this. Would be awesome. Thanks already for reading!
     
  2. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    21
    StartCoroutine using WaitForSeconds then instantiate prefab, endlessly.
    In the prefab, again use StartCoroutine using WaitForSeconds and have it destroy itself.

    Endless examples of StartCoroutine using google.
     
    andrewlewis379 likes this.
  3. andrewlewis379

    andrewlewis379

    Joined:
    Oct 28, 2018
    Posts:
    10
    Thank you i will look into that!
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Excellent, someone who isn't trying to make the next World of Warcraft as their first project. These goals are completely realistic.

    As mentioned you can use a loop in a coroutine to delay execution for a given time before spawning an orb, then repeat indefinitely.

    ie.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawner : MonoBehaviour
    5. {
    6.     public float delay = 10;
    7.  
    8.     private void OnEnable()
    9.     {
    10.         StartCoroutine(SpawningLoop());
    11.     }
    12.  
    13.     private IEnumerator SpawningLoop()
    14.     {
    15.         // continue until the component is disabled
    16.         while(enabled)
    17.         {
    18.             // do stuff
    19.             // ...
    20.  
    21.             // wait for delay seconds before continuing the loop
    22.             yield return new WaitForSeconds(delay);
    23.         }
    24.     }
    25. }

    You can schedule a destruction immediately after spawning using "GameObject.Destroy(newOrbObject, delaySeconds)".

    A perfect bounce can probably be achieved using the physics system alone by setting all orb prefab rigidbody drag to 0, and setting the physics material bounciness to 1, with friction at 0.

    If that still doesn't work, you can programmatically set the velocity, and respond to collisions with a reflected velocity. Since your enemy orbs only live for a minute the physics should be fine I think.
     
    Last edited: Jul 31, 2019
    vakabaka and andrewlewis379 like this.
  5. andrewlewis379

    andrewlewis379

    Joined:
    Oct 28, 2018
    Posts:
    10

    Thanks a lot, will look into this asap. :)
     
  6. andrewlewis379

    andrewlewis379

    Joined:
    Oct 28, 2018
    Posts:
    10
    Thanks guys! After playing around with it for a few hours i figured it out. Thanks a lot!
     
    LiterallyJeff likes this.