プログラミングとイラストレーション » 噴水|Unity5
プログラミングとイラストレーション > Unity > 噴水|Unity5

噴水|Unity5

噴水|Unity5

噴水|Unity5 : ZIPファイル(523kb)

C#

Ball.cs

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {
	//public static float vx = 0;
	//public static float vy = 0;
	public float vx = 0;
	public float vy = 0;
	// Use this for initialization
	void Start () {

	}

	// Update is called once per frame
	void Update () {

	}
}

Fountain.cs

using UnityEngine;
using System.Collections;

public class Fountain : MonoBehaviour {
	float gravity = 0.01f;
	GameObject[] balls;
	// Use this for initialization
	void Start () {
		int length = 100;
		balls = new GameObject[length];
		for (int i = 0; i < length; i++) {
			float x = 0;
			float y = 0;
			Vector2 pos = new Vector2 (x, y);
			GameObject ball = GameObject.Find("ball_small");
			GameObject obj = Instantiate (ball, pos, transform.rotation) as GameObject;

			// パブリック変数
			obj.GetComponent<Ball>().vx = UnityEngine.Random.Range(-0.03f, 0.03f);
			obj.GetComponent<Ball>().vy = UnityEngine.Random.Range(0.1f, 0.3f);

			Color randomColor = new Color( UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value, 1.0f );
			obj.GetComponent<Renderer>().material.color = randomColor;
			balls[i] = obj;
		}
	}

	// Update is called once per frame
	void Update () {
		for (int i = 0; i < balls.Length; i++) {
			GameObject ball = balls [i];
			Vector2 v = ball.transform.position;

			ball.GetComponent<Ball>().vy -= gravity;

			v.x += ball.GetComponent<Ball>().vx;
			v.y += ball.GetComponent<Ball>().vy;

			if (v.x > 4f || v.x < -4f || v.y > 6f || v.y < -6f) {
				v.x = 0;
				v.y = 0;
				ball.GetComponent<Ball>().vx = UnityEngine.Random.Range(-0.03f, 0.03f);
				ball.GetComponent<Ball>().vy = UnityEngine.Random.Range(0.1f, 0.3f);
			}

			ball.transform.position = v;
		}
	}
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e5%99%b4%e6%b0%b4%ef%bd%9cunity5/trackback/