gpt4 book ai didi

c# - 如何更改实例化对象(克隆)的位置?

转载 作者:行者123 更新时间:2023-11-30 18:21:10 24 4
gpt4 key购买 nike

我在我的游戏中创建了一系列球体克隆。之后我调整了比例,使它们看起来更小。但是,现在这些球体之间存在间隙......我将不得不改变这个实例化游戏对象的位置。我已经准确地在这个位置更改了我的代码,但没有任何反应。所以我需要你的帮助!我怎样才能做到这一点?我会有非常小的球体,它们靠得很近。

这里是代码:

using UnityEngine;  
using System.Collections;

public class SineWave : MonoBehaviour {


private GameObject plotPointObject;
private int numberOfPoints= 100;
private float animSpeed =1.0f;
private float scaleInputRange = 8*Mathf.PI; // scale number from [0 to 99] to [0 to 2Pi] //Zahl vor Mathf, Anzahl Bön
private float scaleResult = 2.5f; // Y Achse Range
public bool animate = true;




GameObject[] plotPoints;


// Use this for initialization
void Start () {

if (plotPointObject == null) //if user did not fill in a game object to use for the plot points
plotPointObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); //create a sphere


//add Material to the spheres , load material in the folder Resources/Materials
Material myMaterial = Resources.Load("Materials/green", typeof(Material)) as Material;
plotPointObject.GetComponent<MeshRenderer> ().material = myMaterial;


//change the scale of the spheres
//plotPointObject.transform.localScale = Vector3.one * 0.5f ;
plotPointObject.transform.localScale -= new Vector3(0.5f,0.5f,0.5f);


plotPoints = new GameObject[numberOfPoints]; //creat an array of 100 points.
//plotPointObject.GetComponent<MeshRenderer> ().material =Material.Load("blue") as Material


//plotPointObject.transform.localScale -= new Vector3 (0.5F, 0.5F, 0.5F); //neu: change the scale of the spheres


for (int i = 0; i < numberOfPoints; i++)
{
plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, new Vector3(i -
(numberOfPoints/2), 0, 0), Quaternion.identity); //this specifies
what object to create, where to place it and how to orient it

}
//we now have an array of 100 points- your should see them in the hierarchy when you hit play
plotPointObject.SetActive(false); //hide the original

}

提前谢谢你!

编辑:正如我在评论中所说,我现在实现了将我的领域之间没有间隙的放置。然而,一旦我为我的球体设置动画(使用正弦波),球体之间仍然存在间隙。我该如何适应这个?我应该在更新功能中复制开始功能的代码吗?

我很乐意得到一些帮助。非常感谢!

enter code here void Update()
{
for (int i = 0; i < numberOfPoints; i++)
{
float functionXvalue = i * scaleInputRange / numberOfPoints; // scale number from [0 to 99] to [0 to 2Pi]
if (animate)
{
functionXvalue += Time.time * animSpeed;

}




plotPoints[i].transform.position = new Vector3(i - (numberOfPoints/2), ComputeFunction(functionXvalue) * scaleResult, 0);

//print (plotPointObject.GetComponent<MeshRenderer> ().bounds.size.x);


// put the position information of sphere clone 50 in a vector3 named posSphere
posSphere = plotPoints [50].transform.position;


}

//print position of sphere 50 in console
//print (posSphere);
}


float ComputeFunction(float x)
{
return Mathf.Sin(x);
}




}

最佳答案

我认为您可以制定 Barış 解决方案。对于您要实例化的每个新对象,您会将他的位置设置为最后实例化的位置,加上对象本身的大小,或者您希望它们彼此之间的任何距离。

var initialPosition = 0;
var distanceFromEachOther = 20;
for (int i = 0; i < numberOfPoints; i++) {
var newPos = new Vector3(initialPosition + (i * distanceFromEachOther), 0, 0);
plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, newPos, Quaternion.identity);
}

这将在 X 处的球体之间形成间隙枢轴,取决于它们的大小。更改 distanceFromEachOther var,根据您的需要进行调整。

您还可以使用 plotPointObject.GetComponent<MeshRenderer>().bounds.size 获取物距, 所以 distanceFromEachOther可以是,例如 distanceFromEachOther = plotPointObject.GetComponent<MeshRenderer>().bounds.size.x + 5 .因此,您将拥有彼此之间完美距离为 5 的对象。

关于c# - 如何更改实例化对象(克隆)的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35894846/

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