前提・実現したいこと
緯度経度を基にしてUnity内のオブジェクトを動かしたい
最初に現在地の緯度経度の場所を基準点を基に現実世界とUnity内の空間を縮尺した距離を求めオブジェクトを配置します。次に、その場所から移動先の緯度経度と基準点を基に場所を求め移動させたいです。
double型でオブジェクトの座標を取得したい
Unity内の空間は90m×90m
現実空間は30m×40m
発生している問題・エラーメッセージ
オブジェクトが平面を配置した場所以外のどこかに移動してしまう
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() Location:.ctor(Double, Double) (at Assets/Location.cs:11) Lookdis:Update() (at Assets/Lookdis.cs:63)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Mathd; 5using System; 6 7public class Lookdis : MonoBehaviour 8{ 9 10 //public UnityEngine.UI.Text text; 11 12 //入る変数 13 public GameObject cube; 14 //取得するscriptが入る変数 15 GPS_move script; 16 float speed = 2.0f; 17 //現在地 18 Location Current; 19 //基準 20 //Location reference = new Location(35.4852d, 139.34094d); 21 //付近 22 Location reference=new Location(35.478209d,139.343049d); 23 24 25 Vector3 basepoint; 26 27 private const double EARTH_RADIUS = 6378.137d; // 赤道半径 km 28 private const double POLAR_RADIUS = 6356.752d; // 極半径 km 29 public static double Deg2Rad { get { return Math.PI / 180.0d; } } 30 31 void Start() 32 { 33 Input.location.Start(); 34 cube = GameObject.Find("Cube"); 35 script = cube.GetComponent<GPS_move>(); 36 Current = new Location(script.Latitude, script.Longitude); 37 //緯度1°を求める 38 double x1 = POLAR_RADIUS * 2d * Math.PI / 360.0d; 39 double lat = ((script.Latitude - reference.Latitude)*x1)*1000d; 40 41 //緯度の縮尺 42 double lat1 = (lat * 3d) - 15d; 43 44 //経度1°を求める 45 double z1 = Math.Cos(script.Latitude / 180 * Math.PI) * EARTH_RADIUS; 46 double z2= z1 * 2d * Math.PI / 360.0d; 47 double lon = ((script.Longitude - reference.Longitude) * z2)*1000d; 48 49 //経度の縮尺 50 double lon1 = (lon * 2.25d) - 20d; 51 52 //transform.position = new Vector3d((lat / 4)-31.5, 0, (lon / 4)-31.5); 53 transform.position = new Vector3d(lat1, 5, lon1); 54 } 55 56 // Update is called once per frame 57 void Update() 58 { 59 //Cubeのオブジェクトの位置取得 60 //basepoint = GameObject.Find("Cube").transform.position; 61 62 float step = speed * Time.deltaTime; 63 Current = new Location(script.Latitude, script.Longitude); 64 //緯度1°を求める 65 double x1 = POLAR_RADIUS * 2 * Math.PI / 360.0d; 66 double lat = ((script.Latitude - reference.Latitude) * x1) * 1000; 67 68 //緯度の縮尺 69 double lat1 = (lat * 3) - 15; 70 71 //経度1°を求める 72 double z1 = Math.Cos(script.Latitude / 180 * Math.PI) * EARTH_RADIUS; 73 double z2 = z1 * 2 * Math.PI / 360.0d; 74 double lon = ((script.Longitude - reference.Longitude) * z2) * 1000; 75 76 //経度の縮尺 77 double lon1 = (lon * 2.25) - 20; 78 //Unityない空間の基準点 79 //Vector3 basepoint = new Vector3d(3.5, 0, 3.5); 80 //Vector3 basepoint = new Vector3d(-15, 5, -20); 81 Vector3 devicepoint = new Vector3d(lat1, 5, lon1); 82 83 double tmplat = lat1; 84 double tmplon = lon1; 85 Vector3 basepoint = new Vector3d(tmplat, 5, tmplon); 86 transform.position = Vector3d.MoveTowards(basepoint, devicepoint, step); 87 } 88}
補足情報(FW/ツールのバージョンなど)
Unity2019.4.15f1
あなたの回答
tips
プレビュー