gpt4 book ai didi

c# - 为什么在使用对象池并尝试将对象放在地形上的随机位置时它不是随机的?

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:15 26 4
gpt4 key购买 nike

我有这个池脚本:

using UnityEngine;
using System.Collections.Generic;

public class ObjectPool
{
private GameObject prefab;

private List<GameObject> pool;

public ObjectPool(GameObject prefab, int initialSize)
{
this.prefab = prefab;

this.pool = new List<GameObject>();
for (int i = 0; i < initialSize; i++)
{
AllocateInstance();
}
}

public GameObject GetInstance()
{
if (pool.Count == 0)
{
AllocateInstance();
}

int lastIndex = pool.Count - 1;
GameObject instance = pool[lastIndex];
pool.RemoveAt(lastIndex);

instance.SetActive(true);
return instance;
}

public void ReturnInstance(GameObject instance)
{
instance.SetActive(false);
pool.Add(instance);
}

protected virtual GameObject AllocateInstance()
{
GameObject instance = (GameObject)GameObject.Instantiate(prefab);
instance.SetActive(false);
pool.Add(instance);

return instance;
}
}

我将它与此脚本一起使用来实例化对象。它应该将对象放置在地形区域周围的随机位置。但相反,所有对象都位于 x = 0 、 y = 20 、 z = 0 的相同位置一点都不随机。

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class InstantiateObjects : MonoBehaviour
{

public GameObject Spaceship;
public int spaceshipsStartingHeight = 20;
[HideInInspector]
public GameObject[] spaceships;

// for tracking properties change
private Vector3 _extents;
private int _spaceshipCount;
private float _spaceshipSize;
private List<int> randomNumbers = new List<int>();
private ObjectPool bulletPool;

/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;

/// <summary>
/// How many spheres wanted.
/// </summary>
public int SpaceShipCount;
public float SpaceShipSize;

// Use this for initialization
void Start()
{
rndNumbers();
Clone();
spaceships = GameObject.FindGameObjectsWithTag("SpaceShip");
}

private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
SpaceShipCount = Mathf.Max(0, SpaceShipCount);
SpaceShipSize = Mathf.Max(0.0f, SpaceShipSize);
}

private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
SpaceShipCount = 100;
SpaceShipSize = 20.0f;
}

// Update is called once per frame
void Update()
{

}

private void Clone()
{
if (Extents == _extents && SpaceShipCount == _spaceshipCount && Mathf.Approximately(SpaceShipSize, _spaceshipSize))
return;

// cleanup
var ShipsToDestroy = GameObject.FindGameObjectsWithTag("SpaceShip");
foreach (var t in ShipsToDestroy)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}

var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");

bulletPool = new ObjectPool(Spaceship, SpaceShipCount);

for (var i = 0; i < SpaceShipCount; i++)
{
GameObject o = bulletPool.GetInstance();
o.tag = "SpaceShip";
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(SpaceShipSize, SpaceShipSize, SpaceShipSize);

// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);

// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
var nameToLayer = LayerMask.NameToLayer("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}

//o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
// place !
o.transform.position = new Vector3(x, y + spaceshipsStartingHeight, z);
}

_extents = Extents;
_spaceshipCount = SpaceShipCount;
_spaceshipSize = SpaceShipSize;
}

public void rndNumbers()
{

}
}

最佳答案

答案是删除 Clone() 中的整个//cleanUp 部分。现在它使用池创建随机对象。

关于c# - 为什么在使用对象池并尝试将对象放在地形上的随机位置时它不是随机的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44271822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com