实例化游戏对象并将其放置在错误位置的场景上

时间:2019-12-06 12:31:45

标签: unity3d gps position instantiation gameobject

我正在尝试制作一个Unity app,它将得到实词coordinates,并将其转换为Unity坐标,当我到达该位置时,它必须instantiate并将对象(球体)放在该位置上,问题是它创建了对象,但它仅放置在我站立的位置上。这是我的代码。

  var gpsLat = GPSManager_NoCompass.Instance.latitude;

  var gpsLon = GPSManager_NoCompass.Instance.longitude;

  float latitude = 40.1967097f; // here should be my game object
  float longitude = 44.4802051f;// here should be my game object

    //here is the code to make unity coordinates
  var latOffset = (latitude - gpsLat) * degreesLatitudeInMeters;
  var lonOffset = (longitude - gpsLon) * GetLongitudeDegreeDistance(latitude);

   //placing the sphere
  transform.position = transform.position + new Vector3(latOffset, 0, lonOffset);
  sphere.transform.position = transform.position;
  sphere.transform.localScale = new Vector3(3, 3, 3);
  Instantiate(sphere, sphere.transform.position, Quaternion.identity);

我想问题出在transform position中,因为我需要latOffset和lonOfsset。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我现在无法发表评论,所以我将其发布为答案。

更改最后一部分:

 //placing the sphere
  transform.position = transform.position + new Vector3(latOffset, 0, lonOffset);
  var instantiatedSphere = Instantiate(sphere, transform.position, Quaternion.identity);
  instantiatedSphere .transform.localScale = new Vector3(3, 3, 3);

由于您现在正在使用它,因此您正在为不存在的Sphere分配位置和比例。

答案 1 :(得分:0)

第一行

transform.position = transform.position + new Vector3(latOffset, 0, lonOffset);

移动该脚本所附加的对象!

下一个是您的球体预制件

sphere.transform.position = transform.position;
sphere.transform.localScale = new Vector3(3, 3, 3);

这可能不是您想要的。

您宁愿做类似的事情

var newSpehere = Instantiate(sphere, transform.position + new Vector3(latOffset, 0, lonOffset), Quaternion.identity);
newSphere.localScale = Vector3.one * 3;
相关问题