Skip to content

Instantly share code, notes, and snippets.

@aidiary
Last active October 11, 2018 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aidiary/cd450313aaaa46723234 to your computer and use it in GitHub Desktop.
Save aidiary/cd450313aaaa46723234 to your computer and use it in GitHub Desktop.
ランダムな初速を持つボールオブジェクトを生成する
#pragma strict
// BallSourceController.js
// 生成するボールオブジェクト
// InspectorでBallのPrefabをセットする
var ballObject : GameObject;
function Start () {
// 乱数シードを初期化
Random.seed = System.Environment.TickCount;
}
function OnGUI() {
// GUIメニューを作成
GUI.Box(new Rect(10, 10, 140, 90), "Menu");
// 初期化ボタン
if (GUI.Button(new Rect(20, 40, 120, 20), "Reset")) {
Application.LoadLevel("RigidbodyTest");
}
// ボールオブジェクトを新たに生成
// 初速はランダムにする
if (GUI.Button(new Rect(20, 70, 120, 20), "Create Balls")) {
for (var i = 0; i < 5; i++) {
var clone : GameObject;
clone = Instantiate(ballObject, transform.position, transform.rotation);
clone.rigidbody.velocity.x = 20 * Random.Range(-1.0, 1.0);
clone.rigidbody.velocity.y = 20 * Random.Range(-1.0, 1.0);
clone.rigidbody.velocity.z = 20 * Random.Range(-1.0, 1.0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment