0
在我的突破克隆中,我試圖分裂球,在這一點上他們假定隨機方向移動。這個方向在每個球實例的Start()中決定。經過觀察,似乎我分裂的球越多,所有球向相同方向移動的趨勢越明顯,朝向屏幕的右上角。誰能幫忙?爲什麼每次分球時數值都一樣?這是我的代碼:Unity3D:隨機值顯示一致嗎?
// Use this for initialization
void Start()
{
ballRB = transform.rigidbody;
direction = new Vector3(0,1,0);
//If there's only one ball in the field, being this current one, the default up motion should be used.
if (GameObject.Find("StartBall").GetComponent<StartBall>().numBalls <= 1) ballRB.AddForce(direction*1000f, ForceMode.Force);
//If not, there are more balls, which means the player split the ball. A random direction should be used in this case.
else ballRB.AddForce(GenerateRandomDirection(), ForceMode.Force);
}
Vector2 GenerateRandomDirection()
{
int randX = Random.Range(0,360);
int randY = Random.Range(0,360);
randDirection = new Vector2(randX, randY);
return randDirection;
}
好吧,也許你在'return randDirection;'之前加上'Debug.log(randDirection);'來檢查它們是否相似。在一個基本上像你一樣生成一些Vector2的快速測試中,他們不在哪裏。 –