【发布时间】:2016-06-24 00:28:42
【问题描述】:
我在我的游戏中创建了一系列球体克隆。之后我调整了比例,使它们看起来更小。但是,现在这些球体之间存在间隙......我将不得不改变这个 instatiate 游戏对象的位置。我已经在这个位置更改了我的代码,但没有任何反应。所以请我需要你的帮助!我怎样才能做到这一点?我会有非常小的球体,它们靠近在一起。
代码如下:
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
}
提前谢谢你!
编辑: 正如我在评论中所说,我现在可以将我的球体放置在中间没有间隙。但是,一旦我为球体设置动画(使用正弦波),球体之间仍然存在间隙。我该如何适应这个?我应该将 Start 函数的代码复制到 Update 函数中吗?
我很乐意得到一些帮助。非常感谢!
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);
}
}
【问题讨论】:
-
这不是 C,你是说 C++ 吗?
-
你是对的。代码是用 C# 编写的。我选择 C# 作为标签,但一旦我更新标签,它就会写成 C++。不知道为什么...
-
如果您知道对象的大小,您可以简单地增加价值。例如你只想水平放置物体,在 Y up z front world 中你只能添加 initialposition + i*objectSize。
-
我不明白你的意思,对不起。你能给我一个例子吗。我必须在脚本中的哪个位置进行更改?谢谢。
标签: c# unity3d initialization position scale