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: Imgur: The magic of the Internet
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!
StartCoroutine using WaitForSeconds then instantiate prefab, endlessly.
In the prefab, again use StartCoroutine using WaitForSeconds and have it destroy itself.
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.
Coroutine loop example
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public float delay = 10;
private void OnEnable()
{
StartCoroutine(SpawningLoop());
}
private IEnumerator SpawningLoop()
{
// continue until the component is disabled
while(enabled)
{
// do stuff
// ...
// wait for delay seconds before continuing the loop
yield return new WaitForSeconds(delay);
}
}
}
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.