繁体   English   中英

Unity3d transform.position未返回正确的值

[英]Unity3d transform.position not returning the correct value

关于我正在研究的内容的一些背景知识:基本上,我有一个立方体模型,该模型将用于生成某些地形结构。 所有这些游戏对象都是在运行时通过执行以下操作创建的:

float yPos = 0f;
 float scale = 0.5f;
 var robot = GameObject.Instantiate(grassTile) as GameObject;
 robot.transform.position = new Vector3(0, yPos, 0);
 robot.transform.localScale = new Vector3(scale, scale, scale);
 robot.transform.rotation = Quaternion.Euler(-90, 0, 0);

 width = robot.GetComponent<Renderer>().bounds.size.z;
 for(int i=0;i<5;i++){
     yPos += width;
     robot = GameObject.Instantiate(grassTile) as GameObject;
     robot.transform.position = new Vector3(0, yPos, 0);
     robot.transform.localScale = new Vector3(scale, scale, scale);
     robot.transform.rotation = Quaternion.Euler(-90, 0, 0);
     //attach the script
     robot.AddComponent<CubeBehavior>();
 }

CubeBehavior.cs里面是什么:

void Start () {
     }

     // Update is called once per frame
 void Update () {
         if (Input.touchCount > 0) {
             Debug.Log(transform.position);
         }
 }

无论我触摸哪个多维数据集,发生的事情都将始终返回最后一个多维数据集位置。 请给我有关这个问题的启示。 提前致谢

您没有检测到哪个多维数据集被触摸的代码。

Input.touches仅告诉您当前在屏幕上检测到多少触摸。

要检测触摸,您需要做更多的工作。 一种可能的解决方案是实现触摸事件处理程序。 doc启发示例:

public class CubeBehaviour : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    //Detect current clicks on the GameObject (the one with the script attached)
    public void OnPointerDown(PointerEventData pointerEventData)
    {
        //Output the name of the GameObject that is being clicked
        Debug.Log(name + " click down at position " + transform.position);
    }

    //Detect if clicks are no longer registering
    public void OnPointerUp(PointerEventData pointerEventData)
    {
        Debug.Log(name + "No longer being clicked");
    }
}

这将需要一个EventSystem在您场景中的某个地方。 潜在地,您还需要多维数据集上的触发对撞机,不确定

暂无
暂无

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

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