public float MaxRange = 3f;//范围
var _gameObject = GameObject.Instantiate(prefab);
var InsPos = Random.insideUnitCircle * MaxRange;
float x = InsPos.x + transform.position.x;
float z = InsPos.y + transform.position.z;
var pos = new Vector3(x, 1f, z);
pos.y = Terrain.activeTerrain.SampleHeight(pos);
_gameObject.GetComponent<NavMeshAgent>().Warp(pos); //坐标赋值,瞬间移动
_gameObject.transform.Rotate(new Vector3(0,Random.Range(1f,360f),0)); //随机面向
using UnityEngine;
public class Spawner : MonoBehaviour {
/// <summary>
///是否绘制生成范围球体
/// </summary>
public enum GizmoType { Never, SelectedOnly, Always }
/// <summary>
/// 生成物 prefab
/// </summary>
public Boid prefab;
/// <summary>
/// 生成物体 的 半径范围
/// </summary>
public float spawnRadius = 10;
/// <summary>
/// 孵化数量
/// </summary>
public int spawnCount = 10;
public Color colour;
public GizmoType showSpawnRegion;
void Awake () {
for (int i = 0; i < spawnCount; i++) {
Vector3 pos = transform.position + Random.insideUnitSphere * spawnRadius;
Boid boid = Instantiate (prefab);
boid.transform.position = pos;
boid.transform.forward = Random.insideUnitSphere;
boid.SetColour (colour);
}
}
private void OnDrawGizmos () {
if (showSpawnRegion == GizmoType.Always) {
DrawGizmos ();
}
}
void OnDrawGizmosSelected () {
if (showSpawnRegion == GizmoType.SelectedOnly) {
DrawGizmos ();
}
}
void DrawGizmos () {
Gizmos.color = new Color (colour.r, colour.g, colour.b, 0.3f);
Gizmos.DrawSphere (transform.position, spawnRadius);
}
}