简单的方块堆叠实现

来源:互联网 发布:拜发试剂盒软件 编辑:程序博客网 时间:2024/06/05 04:17


if (Input.GetMouseButtonDown(0))
        {
            Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
           
                if (hit.transform.gameObject.tag.CompareTo("Grid") == 0)  //点击方块
                {
                    cube = hit.transform.gameObject; //获得点击的对象
                    GameObject curgo = Selects();  //获得需要生成的对象

                    Debug.Log(hit.transform.gameObject.name);
                    //editPoints.Add(hit.transform.localPosition);
                    editObjs.Add(curgo);  //将需要生成的对象加入列表
                    GameObject go = CreatNewObj(); //从列表中取出并生成对象
                    go.transform.position = hit.transform.position;
                    go.transform.rotation = hit.transform.rotation;

                    Vector3 direction = hit.point - hit.transform.position;  //第一个点是点击的位置,第二个点是点击对象世界坐标

                    //判断点击的面朝向
                    if ((Mathf.Abs(direction.x)>Mathf.Abs(direction.y)) && (Mathf.Abs(direction.x)>Mathf.Abs(direction.z)))
                       {
                       if (direction.x > 0) {
                             go.transform.Translate (Vector3.right);  //世界坐标  移动一个单位
                         

                        }
                        else if(direction.x < 0){
                            Debug.Log ("x -");
                            go.transform.Translate(Vector3.left);
                          //  go.transform.Rotate(new Vector3(0, 0, 90));

                            //cube.transform.position = hit.transform.position - hit.transform.right;
                        }

                    }

                  if((Mathf.Abs(direction.y)>Mathf.Abs(direction.x)) && (Mathf.Abs(direction.y)>Mathf.Abs(direction.z)))
                  {
                        if (direction.y > 0)
                        {
                            Debug.Log("y +");
                            go.transform.Translate (Vector3.up);
                          //  go.transform.Rotate(new Vector3(0,0,0));
                            //Debug.Log (cube.transform.position+"positiony");
                            //cube.transform.position = hit.transform.position + hit.transform.up;

                        }
                        else if(direction.y <0){
                            Debug.Log ("y -");
                            go.transform.Translate(Vector3.down);
                           // go.transform.Rotate(new Vector3(180, 0, 0));
                           
                        }
       
                    }
                    if((Mathf.Abs(direction.z)>Mathf.Abs(direction.y)) && (Mathf.Abs(direction.z)>Mathf.Abs(direction.x)))
                   {
                        if (direction.z > 0) {
                          Debug.Log ("z +");
       
                           go.transform.Translate (Vector3.forward);
                         //   go.transform.Rotate(new Vector3(90, 0, 0));


                            //Debug.Log (cube.transform.position+"positionz");
                            //cube.transform.position = hit.transform.position + hit.transform.forward;

                        } else if(direction.z <0){
       Debug.Log ("z -"); 
                            go.transform.Translate(Vector3.back);
                         //   go.transform.Rotate(new Vector3(-90, 0,0));

                            //cube.transform.position = hit.transform.position - hit.transform.forward;
                        }

                    }


    }
               
                    _editObjs.Clear();

                }

            }

0 0