Search Unity

Spawning objects in only a certain area

Discussion in 'Scripting' started by Sugarmaster100, Jan 11, 2019.

  1. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
    Hello,
    I would like to spawn an object in a certain are of the play space becuse at the moment i can spawn "coins any where on the board.

    Heres is the code i have for spawning coins
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnObj : MonoBehaviour
    6. {
    7.     public GameObject goldCoin;
    8.     public GameObject bronzeCoin;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetMouseButtonDown(0))
    13.         {
    14.             if (LOGIC.player == "yellow")
    15.             {
    16.                 //Spawns the object Gold
    17.                 Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    18.                 spawnPosition.z = 0.0f;
    19.                 GameObject objectInstance = Instantiate(goldCoin, spawnPosition, Quaternion.Euler(new Vector3(0, 0, 0)));
    20.                 LOGIC.turn++;
    21.             } else if (LOGIC.player == "red")
    22.             {
    23.                 //Spawns the object Bronze
    24.                 Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    25.                 spawnPosition.z = 0.0f;
    26.                 GameObject objectInstance = Instantiate(bronzeCoin, spawnPosition, Quaternion.Euler(new Vector3(0, 0, 0)));
    27.                 LOGIC.turn++;
    28.             }
    29.         }
    30.     }
    31. }
    32.  
    33.  
    34.  
    I have an are where i want the coins to spawn

    I also have code that lets me detect the area of that baord


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(BoxCollider2D))]
    6. public class boxBound: MonoBehaviour
    7. {
    8.     public Vector2 _BoundsMin;
    9.  
    10.     public Vector2 _BoundsMax;
    11.  
    12.     private BoxCollider2D _BoxCol;
    13.  
    14.     void Awake()
    15.     {
    16.         _BoxCol = GetComponent<BoxCollider2D>();
    17.  
    18.         _BoundsMax = _BoxCol.bounds.max;
    19.         _BoundsMin = _BoxCol.bounds.min;
    20.     }
    21. }
    22.  
    Thanks (sorry for reposing noone responded last time)
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  5. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
    I have that with the code, the problem I have is I have no idea how to combine that with instantiate.
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Sugarmaster100

    Well I think it's better you do a bit more reading and learning C# basics.
    But anyway, you could do something like this:

    Code (CSharp):
    1. var bounds = someCollider.bounds;
    2. var px = Random.Range(bounds.min.x, bounds.max.x);
    3. var py = Random.Range(bounds.min.y, bounds.max.y);
    4. Vector2 pos = new Vector3(px, py);
    Then use this to place your instantiated object.
     
  7. Sugarmaster100

    Sugarmaster100

    Joined:
    Nov 13, 2018
    Posts:
    40
    @eses
    sorry to say but that didnt work, i just got a lot of errors.
    upload_2019-1-12_9-39-28.png
     
  8. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Sugarmaster100

    Well var is just to be used in methods... not in class member fields.

    Like i said I think you should go through C# basics tutorial.
     
    brooksjayden312 and acasta like this.