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

Spawn objects at random location in 2d platformer

Discussion in '2D' started by Loud_Lasagna, Nov 18, 2019.

  1. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    i want to spawn chests at random location when game is starting but i don't know the best way to do that
     
  2. Moist_Biscuits

    Moist_Biscuits

    Joined:
    Jul 17, 2019
    Posts:
    4
    Hi, I having a hard time understanding completely what you want to do but I'm going to do my best to help you with your issue. It appears that you might be new to scripting and unity so I'll try to explain how to achieve your problem as best as I can.

    What you are going to need to do is create a Script that you will attach to the object you want to use, in your case a chest that will control how that object behaves. In this case we want to move it to a random position

    To create a script, right click in the "Assets" folder and select Create > C# Script
    Name it whatever you want, but in my case I used the name "RandomObjectExample"

    What I understand is that you want to have script that will create an object at a random location.
    To do this, you are going to want to create 2 random values that will then be used as a vector, which is a pair or values that describe a positon. Then, if you set this random vector to the position of your object (chest) it will move it to a new random location.
    To achieve this Random.Range(x,y) will give us the random values we want in a set range and,
    transform.position = Vector2(x,y) will allow you to move the obejct to a given position.

    Here is an example I made that should hopefully achieve what you want:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomObjectExample : MonoBehaviour
    6. {
    7.     // Random position will be the position we want to place the object
    8.     Vector2 randomPosition;
    9.     public float xRange = 3f;
    10.     // xRange the range in the x axis that the object can be placed
    11.     public float yRange = 3f;
    12.     // yRange the range in the y axis that the object can be placed
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         // xPosition and yPosition are set to random values with the ranges
    18.         float xPosition = Random.Range(0 - xRange, 0 + xRange);
    19.         float yPosition = Random.Range(0 - yRange, 0 + yRange);
    20.         // randomPosition is then given values xPosition and yPosition, making it a random vector
    21.         randomPosition = new Vector2(xPosition, yPosition);
    22.         // randomPosition now describes a random position for our object, so it is then moved to it.
    23.         transform.position = randomPosition;
    24.         // now the object has been moved, completeting the process of placing it in a random position
    25.     }
    26.  
    27. }
    Once you have your script, select the object you want to randomly place and drag the script into the "Inspector" window. From the inspector you will be able to change the range in which that object is placed to whatever you prefer.

    If you need any help with anything that you don't understand do reply to me and I will try my best to help you out.
     
    EricChengjh and vakabaka like this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this is depended on the game. One solution is to use random x-y-position, as Moist_Bisquits said.

    or you can spawn on the predefined positions (just choose one of them)
    Code (CSharp):
    1. public GameObject chest;
    2.     //fill it with empty points on the needed positions
    3.     public Transform[] spawnPosition;
    4.  
    5.     private void Start()
    6.     {
    7.         int i = Random.Range(0, spawnPosition.Length);
    8.         Instantiate(chest, spawnPosition[i].position, spawnPosition[i].rotation);
    9.     }
     
    Loud_Lasagna and MisterSkitz like this.
  4. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    Thank you dude, sorry for lack of explanation and not excellent english but this is not what i'm looking for.
    I want for chest to spawn right over platforms but in random places all over the level. I think i can do this with colliders checking if there something near them.
     
  5. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    And i already know unity basics, so you don't have to explain them to me
     
  6. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    The game is 2d platformer with roguelike elements. Levels have "floors" with different position on x and y axis and different length and height
     
  7. Moist_Biscuits

    Moist_Biscuits

    Joined:
    Jul 17, 2019
    Posts:
    4
    Oh that's great, I didn't mean to be patronising or anytihng, its just that I wasn't sure from the post on your knowledge of scripting and wanted to make sure you understood.

    If you want to have objects randomly appear in a platformer you may find it best to use Random.Range to place them along the top of the game and then have them fall down to the platforms. For example, you could randomise the x part to the component when placing the chest and then give the object gravity by using the RigidBody2D component or your own script. This would cause the chest to be placed onto the platforms as they would be placed at a random x and high y which would cause the chest to fall onto your platofrms

    I understand you have a rougelike which is going to have random elements, but if you have a way of obtaining the length and hieght of the platforms you could also just have the random numbers go along the range of the platform.
     
    Loud_Lasagna likes this.
  8. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    The thing is, the levels are pre-made, only the placement of the objects is random, so I think I can do that. Thank you.
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    If your platforms are prefabs, you can put an empty game object on them all at the same time. Give the empty object a tag "ChestSpawner" then in your code:

    Code (CSharp):
    1. private GameObject[] spawnZones;
    2.  
    3. // inside start()
    4.  
    5. spawnZones = GameObject.FindObjectsWithTag("ChestSpawner");
    Then basically do what @vakabaka suggested above.
     
    vakabaka and Loud_Lasagna like this.
  10. Loud_Lasagna

    Loud_Lasagna

    Joined:
    Oct 24, 2019
    Posts:
    26
    Great thanks, this is working!!!!!!!
     
    MisterSkitz likes this.
  11. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You are welcome, amigo :)