繁体   English   中英

在 Unity 脚本练习和删除对象时遇到问题(非常接近!!)

[英]Having trouble on a Scripting Exercise for Unity and deleting objects (So close!!)

编辑:我已经更新了代码,但它仍然不能正常工作。

我几乎完成了这个项目,但仍停留在最后一部分。

基本上,我正在处理的参数如下:

  1. 在场景中每一帧实例化一个立方体预制件。
  2. 立方体需要根据生成顺序命名为“Cube1”、“Cube2”……。
  3. 立方体需要在距原点 [0,0,0] 1 个单位以内的随机位置生成。
  4. 每个立方体应具有随机颜色。
  5. 立方体大小 (localScale) 在每一帧中缩小 10%。
  6. 当立方体的比例小于其原始比例的 10% 时,立方体将被销毁。

我被困在六个。 立方体被创建、着色和命名,并随着时间的推移而缩小,但在缩小到一定程度后并不总是会自我毁灭。 我已经让他们删除了一些立方体,但其他立方体现在将停止收缩并无限期地留在场景中。

如果这有帮助,我注意到在删除某个元素(例如 cube0)后,它将再次重用该名称,然后对象列表看起来一团糟,并且停止删除。

到目前为止我的代码:

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeCreator : MonoBehaviour
{
    Vector3 origin;
    private List<GameObject> cubeList;
    public float targetScale = 0.1f;
    public float shrinkSpeed = 0.1f;
    GameObject newCube;
// Start is called before the first frame update




private void Awake()
{
    cubeList = new List<GameObject>();
}

void Start()
{
    origin = new Vector3(0, 0, 0);

}

// Update is called once per frame
void Update()
{
    AddNewCube();
    for (int i = 0; i < cubeList.Count; i++)
    {
        cubeList[i].name = "cube" + i;
        cubeList[i].transform.localScale = Vector3.Lerp(cubeList[i].transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
        if (cubeList[i].transform.localScale.x <= targetScale + 0.5f)
        {
            cubeList.RemoveAt(i);
            RemoveCube(newCube);
            print("cube " + i + " removed");

        }
    }
}

private void SetRandCubeState(GameObject cube)
{

    float randScale = Random.Range(0.8f, 1.0f);
    Vector3 randomScaler = new Vector3(randScale, randScale, randScale);
    cube.transform.localScale = randomScaler;
    Vector3 randomPosition = new Vector3(Random.Range(origin.x - 1.0f, origin.x + 1.0f), Random.Range(origin.y - 1.0f, origin.y + 1.0f), Random.Range(-1.0f,1.0f));
    cube.transform.localPosition = randomPosition;
    Renderer render = cube.GetComponent<Renderer>();
    render.material.SetColor("_Color", Random.ColorHSV());

}

private void AddNewCube()
{
    GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cubeList.Add(newCube);
    SetRandCubeState(newCube);
    


}

private void RemoveCube(GameObject oldCube)
{
    Destroy(oldCube);

}

}

请注意,这些任务并未提及随机初始比例。 它进一步说立方体应该在达到原始规模的< 10%后销毁。 因此,如果您使用随机初始比例,那么您还需要将原始比例存储在某处。

它还说立方体应该10% each frame而不是基于秒数。 所以我是如何理解它的,第一帧是1 -> 0.9 ,第二帧是0.9 -> 0.81等等,所以任务制定不当或者你正在做其他事情 ^^

Lerp取一个介于01之间的因子,线性插值相应地开始和结束值。 你用错了^^

假设每秒 60 帧,你一直在当前比例和0.1之间插值,(或多或少是常数)因子约为0.0017 .. 你的立方体几乎不会缩小并且变得越来越慢,你越接近目标。 绝对不会每帧缩小10 % ;)您还对值0.1而不是0进行了 lerping。 因此,您的阈值可能根本不会达到,或者只会非常非常慢。

此外,多维数据集应根据它们创建的顺序命名 -> 你不应该遍历列表并重命名它们。

// Store existing cube references together with their minimum scale
// before getting destroyed
private Dictionary<Transform, float> cubes = new Dictionary<Transform, float>();

// Count created objects for correct naming
private int cubeSpawnCounter;

void Update()
{
    AddNewCube();

    foreach(var kvp in cubes)
    {
        // Scale down 10% per frame
        kvp.Key.localScale *= 0.9f;

        // Check if reached below threshold of 10% of original scale
        if (kvp.Key.localScale.x <= kvp.Value)
        {
            RemoveCube(kvp.Value);
        }
    }
}

private void SetRandCubeState(GameObject cube)
{
    // Multiply the vector that saves you a lot of typing ;)
    cube.transform.localScale = Vector3.one * Random.Range(0.8f, 1.0f);

    // storing the origin is redundant
    cube.localPosition = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f), Random.Range(-1.0f,1.0f));

    // Only go via the Shader properties if really necessary
    var render = cube.GetComponent<Renderer>();
    render.material.color = Random.ColorHSV());
}

private void AddNewCube()
{
    var newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);

    SetRandCubeState(newCube);

    // Count the created cubes and set the name ONCE
    cubeSpawnCounter++;
    newCube.name = $"Cube{cubeSpawnCounter}";

    // store the destroy threshold for each cube (10% of original scale)
    cubes[newCube.transform] = randomScale * 0.1f;
}

private void RemoveCube(Transform oldCube)
{
    // Remove from the Dictionary
    cubes.RemoveKey(oldCube);
    print($"{oldCube.name} removed");
    Destroy(oldCube.gameObject);
}

由于 lerp,该值似乎永远不会达到 1。 您可以尝试在条件检查中添加一个浮点数(我使用了一个很大的数字,但是 using.01f 可以根据您的用例工作)

if(cubeList[i].transform.localScale.x <= targetScale + .3f) 
{
    cubeList.RemoveAt(i);
    RemoveCube(newCube);
    print("cube " + i + " removed");

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM