プログラミングとイラストレーション » 複数のバネ|Unity5
プログラミングとイラストレーション > Unity > 複数のバネ|Unity5

複数のバネ|Unity5

複数のバネ|Unity5

複数のバネ|Unity5 : ZIPファイル(560kb)

3つの黒い点をドラッグして、中央の白い点との間に結ばれたバネの動きを確認できます。

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 () {

	}
}

MultiSpring.cs

using UnityEngine;
using System.Collections;

public class MultiSpring : MonoBehaviour {

	GameObject ball_white;
	GameObject[] handles;
	float spring = 0.1f; // バネ係数
	float friction = 0.8f; // 摩擦係数
	int numHandles = 3; // ボールの数
	LineRenderer renderer1;

	float offsetX;
	float offsetY;

	// Use this for initialization
	void Start () {
		renderer1 = gameObject.GetComponent<LineRenderer>();
		// 頂点の数
		renderer1.SetVertexCount(6);
		// 線の幅
		renderer1.SetWidth(0.025f, 0.025f);
		renderer1.material = new Material (Shader.Find ("Particles/Additive"));
		renderer1.enabled = false;

		GameObject ball = GameObject.Find("ball_small");
		Vector2 ball_pos = ball.transform.position;
		// 白いボールのインスタンス作成
		ball_white = Instantiate (ball, ball_pos, ball.transform.rotation) as GameObject;
		ball_white.GetComponent<CircleCollider2D> ().enabled = false; // 衝突判定の無効化
		handles = new GameObject[numHandles];

		for (int i = 0; i < numHandles; i++) {
			GameObject handle = GameObject.Find("ball_small");
			Vector2 pos = handle.transform.position; 
			pos.x = UnityEngine.Random.Range(-3f, 3f);
			pos.y = UnityEngine.Random.Range(-5f, 5f);

			// 黒いボールのインスタンス作成
			GameObject obj = Instantiate (handle, pos, handle.transform.rotation) as GameObject;

			// カラー設定
			Color color = new Color( 0/255.0f, 0/255.0f, 0/255.0f, 1.0f );
			obj.GetComponent<Renderer>().material.color = color;
			obj.GetComponent<Renderer> ().name = "ball" + i;
			handles[i] = obj;
		}
	}
	
	// Update is called once per frame
	void Update () {
		Vector2 ball_pos = ball1.transform.position;

		for (int i = 0; i < numHandles; i++) {
			GameObject handle = handles [i];
			Vector2 pos = handle.transform.position;

			float dx = pos.x - ball_pos.x;
			float dy = pos.y - ball_pos.y;

			ball_white.GetComponent<Ball> ().vx += dx * spring;
			ball_white.GetComponent<Ball> ().vy += dy * spring;
		}

		ball_white.GetComponent<Ball> ().vx *= friction;
		ball_white.GetComponent<Ball> ().vy *= friction;

		ball_pos.x += ball_white.GetComponent<Ball> ().vx;
		ball_pos.y += ball_white.GetComponent<Ball> ().vy;

		ball_white.transform.position = ball_pos;

		renderer1.enabled = true;
		int j = -1;
		for (int i = 0; i < 6; i++) {
			if (i % 2 == 0) {
				renderer1.SetPosition (i, new Vector2 (ball_white.transform.position.x, ball1.transform.position.y));
			} else {
				j++;
				renderer1.SetPosition (i, new Vector2 (handles[j].transform.position.x, handles[j].transform.position.y));
			}
		}
			
		if(Input.GetMouseButtonDown(0)){
			Vector2 tapPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			Collider2D aCollider2d = Physics2D.OverlapPoint(tapPoint);

			if (aCollider2d) {
				int num = 0;
				for (int i = 0; i < handles.Length; i++) {
					if (aCollider2d.name == handles[i].name) {
						num = i;
					}
				}
				Vector2 spritePos = handles [num].transform.position;
				Vector2 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

				float dx = mousePos.x - spritePos.x;
				float dy = mousePos.y - spritePos.y;
				float rad = Mathf.Atan2 (dy, dx);
				float dist = Mathf.Sqrt (dx * dx + dy * dy);
				offsetX = Mathf.Cos (rad) * dist;
				offsetY = Mathf.Sin (rad) * dist;
				
			}
		}

		if(Input.GetMouseButton(0)){
			Vector2 tapPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			Collider2D aCollider2d = Physics2D.OverlapPoint(tapPoint);
			if (aCollider2d) {
				int num = 0;
				for (int i = 0; i < handles.Length; i++) {
					if (aCollider2d.name == handles[i].name) {
						num = i;
					}
				}
				Vector2 sprite = handles [num].transform.position;
				Vector2 mouseMovePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

				sprite.x = mouseMovePos.x - offsetX;
				sprite.y = mouseMovePos.y - offsetY;
				handles [num].transform.position = sprite;
			}
		}

		if(Input.GetMouseButtonUp(0)){

		}
	}
}

コメントを残す

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

コメントフィード

トラックバック URL : http://www.htmlcode.jp/%e8%a4%87%e6%95%b0%e3%81%ae%e3%83%90%e3%83%8d%ef%bd%9cunity5/trackback/