2点間の距離|Unity5

2点間の距離|Unity5

2点間の距離|Unity5 : ZIPファイル(517kb)

C#

Distance.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Distance : MonoBehaviour {
	public Text infoText;

	// Use this for initialization
	void Start () {
		GameObject ball1 = GameObject.Find("ball1");
		GameObject ball2 = GameObject.Find("ball2");
		Vector2 v = ball1.transform.position;
		v.x = UnityEngine.Random.Range (-2f, 2.5f);
		v.y = UnityEngine.Random.Range (-4f, 4f);
		ball1.transform.position = v;

		v = ball2.transform.position;
		v.x = UnityEngine.Random.Range (-2f, 2.5f);
		v.y = UnityEngine.Random.Range (-4f, 4f);
		ball2.transform.position = v;


		float dx = ball1.transform.position.x - ball2.transform.position.x;
		float dy = ball1.transform.position.y - ball2.transform.position.y;
		float dist = Mathf.Sqrt (dx * dx + dy * dy);
		infoText.text = "キョリ: " + dist.ToString();
		//Debug.Log("距離: " + dist);
	}

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

	}
}

コメントを残す

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