Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by redfire082 · Sep 18, 2014 at 05:34 PM · c#instantiateplatformerpowerup

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePunisher · Sep 18, 2014 at 06:13 PM 0
Share

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.

avatar image redfire082 · Sep 19, 2014 at 08:46 AM 0
Share

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) ;
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges