将游戏对象旋转到北方

时间:2017-11-16 16:57:31

标签: c# android google-maps unity3d compass

我想将地图旋转到北方,但它会一直旋转而不会停在北方。我想要一个像谷歌地图一样的输出,地图的旋转固定在北方。请检查我的Update()方法。点击此处观看视频:https://www.youtube.com/watch?v=liXmxdI3Adg&feature=youtu.be

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

public class GoogleAPI : MonoBehaviour {
    private string url;
    public RawImage img;
    public float latitude;
    public float longitude;
    public Text text;
    public Text northtext;

    private IEnumerator Map(float lat, float lon){

        url = "https://maps.googleapis.com/maps/api/staticmap?key=AIzaSyAG0hDd5mZNla3GFCi0cs9wWTkWu-eBBWA&center="+lat+","+lon+"&zoom=19&format=png&maptype=roadmap&style=element:geometry%7Ccolor:0x1d2c4d&style=element:labels.text.fill%7Ccolor:0x8ec3b9&style=element:labels.text.stroke%7Ccolor:0x1a3646&style=feature:administrative.country%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:administrative.land_parcel%7Celement:labels.text.fill%7Ccolor:0x64779e&style=feature:administrative.province%7Celement:geometry.stroke%7Ccolor:0x4b6878&style=feature:landscape.man_made%7Celement:geometry.stroke%7Ccolor:0x334e87&style=feature:landscape.natural%7Celement:geometry%7Ccolor:0x023e58&style=feature:poi%7Celement:geometry%7Ccolor:0x283d6a&style=feature:poi%7Celement:labels.text%7Cvisibility:off&style=feature:poi%7Celement:labels.text.fill%7Ccolor:0x6f9ba5&style=feature:poi%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:poi.business%7Cvisibility:off&style=feature:poi.park%7Celement:geometry.fill%7Ccolor:0x023e58&style=feature:poi.park%7Celement:labels.text.fill%7Ccolor:0x3C7680&style=feature:road%7Celement:geometry%7Ccolor:0x304a7d&style=feature:road%7Celement:labels.icon%7Cvisibility:off&style=feature:road%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:road%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:road.highway%7Celement:geometry%7Ccolor:0x2c6675&style=feature:road.highway%7Celement:geometry.stroke%7Ccolor:0x255763&style=feature:road.highway%7Celement:labels.text.fill%7Ccolor:0xb0d5ce&style=feature:road.highway%7Celement:labels.text.stroke%7Ccolor:0x023e58&style=feature:transit%7Cvisibility:off&style=feature:transit%7Celement:labels.text.fill%7Ccolor:0x98a5be&style=feature:transit%7Celement:labels.text.stroke%7Ccolor:0x1d2c4d&style=feature:transit.line%7Celement:geometry.fill%7Ccolor:0x283d6a&style=feature:transit.station%7Celement:geometry%7Ccolor:0x3a4762&style=feature:water%7Celement:geometry%7Ccolor:0x0e1626&style=feature:water%7Celement:labels.text.fill%7Ccolor:0x4e6d70&size=360x640&scale=2";
        WWW www = new WWW (url);
        yield return www;
        img.texture = www.texture;
        img.SetNativeSize ();
    }

    private IEnumerator StartLocationService(){
        if (!Input.location.isEnabledByUser) {
            Debug.Log ("enable gps pls");
            yield break;
        }

        Input.location.Start ();

        int maxWait = 20;
        while(Input.location.status == LocationServiceStatus.Initializing && maxWait > 0){
            yield return new WaitForSeconds (1);
            maxWait--;
        }

        if(maxWait <=0 ){
            Debug.Log ("Expired");
            yield break;
        }

        if (Input.location.status == LocationServiceStatus.Failed) {
            Debug.Log ("Unable to determine device location");
            yield break;
        }



        latitude = Input.location.lastData.latitude;
        longitude = Input.location.lastData.longitude;

        yield break;
    }
    // Use this for initialization
    void Start () {
        img = img.GetComponent<RawImage> ();
        StartCoroutine (StartLocationService());
        StartCoroutine (LoadData());
        Input.gyro.enabled = true;
        Input.compass.enabled = true;
    }

    private IEnumerator LoadData(){
        while(true){
            latitude = Input.location.lastData.latitude;
            longitude = Input.location.lastData.longitude;
            text.GetComponent<Text> ().text = "Lat: "+latitude+"\nLon: "+longitude;
            StartCoroutine (Map(latitude, longitude));
            yield return new WaitForSeconds(1);
        }
    }

    void Update(){
        img.GetComponent<RawImage> ().transform.Rotate (0,0,Convert.ToInt16(Input.compass.trueHeading));
        northtext.GetComponent<Text> ().text = Convert.ToInt16(Input.compass.trueHeading).ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

您无限旋转而不是设置旋转。

您可以这样做:

void Update() 
{
    var targetEuler = new Vector3(0,0,Input.compass.trueHeading);
    img.transform.rotation = Quaternion.Lerp(image.transform.rotation, targetEuler, Time.deltaTime);
}