Search Unity

Spawning objects in only a certain area

Discussion in 'Scripting' started by Sugarmaster100, Jan 9, 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
    upload_2019-1-9_12-5-2.png
    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
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311