- Home /
spawn a powerup in 2D platformer c#
This is my first project so please bear with me. I've managed to create a scene which works pretty well - it has floor, player, collisions, physics and some mechanics for decreasing health.
My problem stems from a powerup/powerdown mechanic. I have : 1 prefab (a question mark) which applies a random powerup/powerdown to the player. When player collides, it applies the affect for 5 secs. I have tested this code and works well (using switch statement for random affect).
I have looked everywhere and cannot find a good example to achieve my last part. This is to:
Spawn the prefab x number of times throughout the level just above the floor (floor is rectangle collider). The X position can be anything, but the Y value needs to be just above the floor.
I need the powerup/down prefab to not overlap anything (including itself) and it needs to be random - i.e. not using a hidden game object for spawn points.
The floor is on it's own layer which I thought could assist me ... psedo code I thought:
if (spawnpoint above floor_layer)
instantiate (powerup, random.range.x, floor_layer.y)
Any advice would be greatly appreciated on this. I looked at raycasting (haven't really been successful in implementing raycasting though), manual spawn points (not what I prefer).
End result should look like: 1) player starts level 2) as player runs through, powerups/downs appear throughout the level at ground level. 3) my powerup script destroys prefab after applying affect - should remain destroyed.
Is there a particular reason why you don't want to place spawn points manually for these powerups? It's a much simpler mechanic to implement and unless your levels are randomly generated there is no need to do this dynamically. You can always add a bunch of spawners in areas you think would be good candidates for power-ups, then randomly choose a subset of them. For example, you may have 10 spawn points but you only ever randomly choose 3 of them when the level starts.
Thanks for both of your responses.
@ThePunisher - I agree that spawn points will be easier and have considered it. $$anonymous$$y reason for not using them is that I plan the level to be very long and would prefer the powerups to spawn in different places each time - the player would hopefully have to replay the level numerous times and having pickups in different locations should add to a more varied game experience.
@Jopan - Thanks for the code - it's very similar to the one I'm using now. The pickups spawn under my platforms though - this is due to my platforms elevating throughout the scene so Y co-ords can vary alot. The code I would need will require a check to see if the ground is beneath the spawning X location - if spawnlocation.X > ground.y then spawn object, etc.
As a temporary stop gap I've created a different method of spawning - the powerups spawn well above the scene with a rigidbody and child object with another collider (due to the parent collider being a trigger). What happens is: Powerup spawns out of screen. Powerups drop down and land on platform. $$anonymous$$y current issue is trying to make the powerups not "slide" down the slopes in the level - they should drop and stay still ready for pickup.
Below is the simple code I'm using for this method which has potential with a little tweaking:
public class PudSpawnScript : $$anonymous$$onoBehaviour {
public GameObject pudPrefab;
public GameObject groundObject;
void Update () {
for (int i = 0; i < 10; i++) {
SpawnPud ();
}
}
private void SpawnPud ()
{
Instantiate (pudPrefab, new Vector3(Random.Range(-28, 200), 20), pudPrefab.transform.rotation) ;
}
Answer by Jopan · Sep 18, 2014 at 05:50 PM
using UnityEngine;
using System.Collections;
public class SpawnPowerUps : MonoBehaviour
{
public int numberOfPowerUps = 10; // How many power ups to spawn
public float minXSpawn = 0f; // The min x position that power ups can spawn
public float maxXSpawn = 10f; // The max x position that power ups can spawn
public float ySpawn = 1f; // The y position that all power ups will spawn
public GameObject powerUpPrefab; // The prefab of the power up to be spawned
void Start()
{
// Run code numberOfPowerUps amount of times
for (int i = 0; i < numberOfPowerUps; i++)
{
// Generate a random number between minXSpawn and maxXSpawn
float randomX = Random.Range(minXSpawn, maxXSpawn);
Vector2 spawnPosition = Vector2.zero; // Create a variable to store the spawn position being generated
spawnPosition.x = randomX; // Assign x to be our random x value
spawnPosition.y = ySpawn; // Assign y to our desired y position
// Instantiate our powerup at the spawn position with a default rotation
Instantiate(powerUpPrefab, spawnPosition, Quaternion.identity);
}
}
This won't solve the power ups not spawning on each other, but it does all the other stuff you want.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# - instance is not set to an instance of an object 2 Answers
instantiates in the same place 2 Answers
Multiple Cars not working 1 Answer
Destroying an instantiated object colliding with another instantiated object 3 Answers