简体   繁体   English

Unity3D:如何确定游戏对象的角落,以便根据它来定位其他游戏对象?

[英]Unity3D: How to determine the corners of a gameobject in order to position other gameobjects according to it?

My question is about if there is a way to know the coordinates of the corners of a gameobject. 我的问题是关于是否有办法知道游戏对象角落的坐标。 What I have is three Vuforia AR targets and a gameobject, a cube in this case. 我所拥有的是三个Vuforia AR目标和一个游戏对象,在这种情况下是一个立方体。

What I need to achieve, is so that when I move the targets around, the corners of the cube would follow the targets, eg. 我需要实现的是,当我移动目标时,立方体的角落将跟随目标,例如。 the cube would be as wide as the space between the targets. 立方体将与目标之间的空间一样宽。

Right now how it does it, is that it checks the distance between the targets and sets the scale of the cube according to it. 现在它是如何做到的,它是检查目标之间的距离并根据它设置立方体的比例。 This results in the cube being expanded always from its set position, which makes the positioning of the targets awkward in real life. 这导致立方体总是从其设定位置扩展,这使得目标的定位在现实生活中变得笨拙。

Here's a couple of pictures showing what it does now, taken during execution: 这里有几张图片展示了它在执行过程中的作用:

Here is the code attached to the cube-object: 这是附加到cube-object的代码:

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);
}
}

How do I make the corners of the object follow the targets? 如何使对象的角落跟随目标? I need the width of the side to be the width of the distance between the targets, is there any way to achieve this without using the scale? 我需要将边的宽度作为目标之间距离的宽度,有没有办法在不使用刻度的情况下实现这一点?

I know this is quite some time after you asked the question, but I came across this as I was looking to sort something similar out myself. 我知道在你问这个问题之后已经有一段时间了,但是当我想要对自己进行类似的事情时,我发现了这一点。

What I found I needed (and others may be helped) is to use Renderer.bounds 我发现我需要(其他人可能会得到帮助)就是使用Renderer.bounds

What it looks like in practice for me: 在实践中对我来说是什么样的:

void Update () {
  rend = currentGameObject.GetComponent<Renderer>();
  Debug.Log(rend.bounds.max);
  Debug.Log(rend.bounds.min);
}

My object in this case was a quad at position 0,0,200 and a scale of 200,200,1. 在这种情况下,我的目标是位于0,0,200位置的四边形和200,200,1的标度。 The output of rend.bounds.max is (100.0,100.0,200.0) the min was (-100.0,-100.0,200.0) . rend.bounds.max的输出为(100.0,100.0,200.0) ,min为(-100.0,-100.0,200.0) This gives you the corner position for each corner (granted my example was in 2D space, but this should work for 3d as well). 这为您提供了每个角落的角落位置(我的示例是在2D空间中,但这也适用于3D)。

To get it a little more specific for your example if you wanted the top right corner, you could get the XY of renderer.bounds.max , for the top left you would get the renderer.bounds.max.y and the renderer.bounds.min.x . 为了让你的例子更具体一点,如果你想要右上角,你可以得到renderer.bounds.max的XY,左上角你会得到renderer.bounds.max.yrenderer.bounds.min.x Hope that helps! 希望有所帮助!

Cheers! 干杯!

  1. Create 8 empty game objects. 创建8个空游戏对象。
  2. Make them children of the "cube" object to be tracked. 让他们成为要跟踪的“立方体”对象的子项。
  3. Move them in Editor to be at the 8 corners of your tracked game object. 将它们在编辑器中移动到跟踪的游戏对象的8个角落。 Since they are children of the tracked object, their positions will be {+/- 0.5, +/- 0.5, +/- 0.5} . 由于它们是被跟踪物体的子项,因此它们的位置将为{+/- 0.5, +/- 0.5, +/- 0.5}
  4. Name them memorably (ie, "top-left-back corner"). 命名他们(即“左上角”)。
  5. These 8 objects will move and scale with the tracked object, staying at its corners. 这8个物体将随被跟踪物体移动和缩放,停留在其角落。 Any time you want to know where a corner is, simply reference one or more of the 8 game objects by name and get the Vector3 for each of their transform.position . 每当你想知道角落的位置时,只需按名称引用8个游戏对象中的一个或多个,并获得每个transform.positionVector3

Note: If you're doing step 5 repeatedly, you may want to avoid potentially costly GameObject.Find() calls (or similar) each time. 注意:如果您反复执行第5步,则可能希望每次都避免可能代价高昂的GameObject.Find()调用(或类似)。 Do them once and save the 8 game objects in a variable. 做一次并将8个游戏对象保存在变量中。

I worked my way around the problem by determining the position between the targets and counting their mean. 我通过确定目标之间的位置并计算它们的平均值来解决问题。 This is not flawless, however, since it is difficult to determine the scale factor with which the models would accurately follow the targets' corners, when bringing external models to use with the system. 然而,这并非完美无瑕,因为当将外部模型用于系统时,很难确定模型准确跟随目标角落的比例因子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM