- Home /
Question by
gw_unity · Oct 22, 2018 at 11:21 PM ·
spawningspawning problemsrandomspawning
My game object that spawns randomly threw out my game sometimes spawns inside a tree
So my game spawns an object threw out my game randomly each time and sometime it spawns in a tree anyone know a fix for this so it doesn't spawn in a tree. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnObject : MonoBehaviour {
public Vector3 center;
public Vector3 size;
public GameObject KeyPrefab;
// Use this for initialization
void Start () {
SpawnKey();
}
// Update is called once per frame
void Update () {
}
public void SpawnKey()
{
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x /2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
Instantiate(KeyPrefab, pos, Quaternion.identity);
}
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1,0,0,0.5f);
Gizmos.DrawCube(center, size);
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple spawn location with random game object in random time spawn c# 1 Answer
Side scroller spawns at different Y 1 Answer
[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers
Why doesn't my spawning code work? 2 Answers
How do I make enemies not spawn in the same position? 2 Answers