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. Dismiss Notice

Creating a randomly spawning point system for 2D Side-scroller

Discussion in 'Scripting' started by MCamac55, Jan 8, 2015.

  1. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    So, im creating a game where I need to have objects spawn on the right side of the screen at random points of the Y-Axis then fly to the left to be collected by the player. How would I go about coding this in C#?
     
  2. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Get the player X position and add a certain amount to it so it's outside the camera(or create an empty game object outside the camera and attach it to your player and just get the x position of it.), then create a random Y position with Random.Range. Instantiate your game object using the X and Y coordinates you just made. To make it move left use transform.translate and a vector.

    http://docs.unity3d.com/ScriptReference/Random.Range.html
    http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    http://docs.unity3d.com/ScriptReference/Transform.Translate.html
     
    Last edited: Jan 8, 2015
  3. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    I really have no idea how I would put that into code.
     
  4. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Note: I haven't tested this code since I don't have access to any compiler at the moment, but it should give you the basic idea

    Code (CSharp):
    1.  
    2. public GameObject MyPrefab; //Assign your prefab here
    3. public GameObject Player; //Assign your player object or camera here just to get the coordinates (if your player is attached to the camera it doesn't matter, however if you can move around without your camera moving you want to use your camera)
    4. public GameObject PickUp; //this is the object you will spawn
    5.  
    6. public int Speed; //Speed determines how fast your object moves
    7.  
    8. void SpawnMyPrefab(){
    9.        int y = Random.Range(Number1, Number2); //Number1 is min, Number2 is max. Change this to whatever you need. (Note: if your terrain goes up and down you want to use Player.transform.position.y with random.range to get a good value so it doesn't spawn too high or low.
    10.  
    11.        float x = Player.transform.position.x + 40f; //Adds 40 to Player.position.x (make sure 40 is enough for it to be outside your camera)
    12.  
    13. //Another note! if you used an empty game object that has been attached to your player/camera and you moved it outside the screen, all you need is Player.transform.position.x since it is already outside the screen.
    14.  
    15. //After this you want to spawn your game object:
    16.  
    17.        PickUp = Instantiate (MyPrefab, new Vector3(x, y, 0), Quaternion.identity) as GameObject; //Vector3 is just incase you need to change the Z coordinate, but usually 0 is all you need in 2D games, assuming your character has 0 too. You can use Vector2 if you want to.
    18.  
    19. }
    20.  
    21. void MoveMyPrefab(){
    22.          PickUp.transform.Translate(Vector2.right * -Speed * Time.deltaTime); //Negative Speed turns vector2.right around so it becomes left (or at least should, i'm writing this without testing anything)
    23. }
    24.  
    25.  
    Could be done better, may have some errors since I couldn't test this but this should get you started or if anything give you the idea of what to do.
     
    Last edited: Jan 8, 2015
  5. MCamac55

    MCamac55

    Joined:
    Jan 8, 2015
    Posts:
    22
    Thanks for the code. It had a few basic errors. nothing that I couldn't fix. What would I put in the prefabs?
     
  6. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    You got the prefabs in the editor? just assign the game object you want to spawn to them.

    If this is a problem for you just google it, there are many tutorials for this.