我的问题是关于是否有办法知道游戏对象角的坐标。我拥有的是三个 Vuforia AR 目标和一个游戏对象,在本例中是一个立方体。
我需要实现的是,当我四处移动目标时,立方体的角将跟随目标,例如。立方体将与目标之间的空间一样宽。
现在它是如何做到的,它检查目标之间的距离并根据它设置立方体的比例。这导致立方体总是从其设定位置扩展,这使得目标的定位在现实生活中很尴尬。
这里有几张图片展示了它现在所做的事情,在执行过程中拍摄:
这是附加到立方体对象的代码:
using UnityEngine;
using System.Collections;
public class show : MonoBehaviour {
float distancex;
float distancez;
// Use this for initialization
void Start () {
renderer.enabled = false;
}
// Update is called once per frame
void Update () {
if (GameObject.Find ("target1").renderer.enabled && GameObject.Find ("target2").renderer.enabled &&
GameObject.Find ("target3").renderer.enabled && GameObject.Find ("target4").renderer.enabled)
{
renderer.enabled = true;
}
distancex = Vector3.Distance ((GameObject.Find ("target1").transform.position), (GameObject.Find ("target2").transform.position));
distancez = Vector3.Distance ((GameObject.Find ("target2").transform.position), (GameObject.Find ("target3").transform.position));
Debug.Log (distancex);
Debug.Log (distancez);
transform.localScale = new Vector3((distancex), transform.localScale.y, transform.localScale.z);
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, (distancez));
Debug.Log (transform.localScale);
}
}
如何使对象的角跟随目标?我需要边的宽度为目标之间距离的宽度,有没有什么方法可以不使用刻度来实现这一点?
最佳答案
我知道你问这个问题已经有一段时间了,但我遇到这个问题是因为我想自己解决类似的问题。
我发现我需要的(其他人可能会得到帮助)是使用 Renderer.bounds
对我来说在实践中是什么样的:
void Update () {
rend = currentGameObject.GetComponent<Renderer>();
Debug.Log(rend.bounds.max);
Debug.Log(rend.bounds.min);
}
在这种情况下,我的对象是位置为 0,0,200 且比例为 200,200,1 的四边形。 rend.bounds.max 的输出是 (100.0,100.0,200.0)
,最小值是 (-100.0,-100.0,200.0)
。这为您提供了每个角的角位置(假设我的示例是在 2D 空间中,但这也适用于 3d)。
如果你想要右上角,为了让它更具体一点,你可以获得 renderer.bounds.max
的 XY,对于左上角,你将获得 renderer.bounds.max.y
和 renderer.bounds.min.x
。希望对您有所帮助!
干杯!
关于c# - Unity3D:如何确定游戏对象的角以根据它定位其他游戏对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605683/