Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果节点不为null,如何防止拖放[C#]_C#_Unity3d_Drag And Drop - Fatal编程技术网

如果节点不为null,如何防止拖放[C#]

如果节点不为null,如何防止拖放[C#],c#,unity3d,drag-and-drop,C#,Unity3d,Drag And Drop,我对游戏中的拖放操作有问题。我想阻止用户,如果他是从下到上拖动对象的情况下,他们有相同的角度棒。如果他们不这样做,那没关系。但若它们在同一方向上有一根棍子,那个么棍子应该回到第一个位置 我有点迷路了。你能帮忙吗:) 这是我拖放旋转的代码 使用System.Collections.Generic; 使用UnityEngine 公共类InputController:MonoBehavior { 浮动点击时间; RayCastHit 2D rayhit; 浮动最小距离=1.5f; int small

我对游戏中的拖放操作有问题。我想阻止用户,如果他是从下到上拖动对象的情况下,他们有相同的角度棒。如果他们不这样做,那没关系。但若它们在同一方向上有一根棍子,那个么棍子应该回到第一个位置

我有点迷路了。你能帮忙吗:)

这是我拖放旋转的代码

使用System.Collections.Generic; 使用UnityEngine

公共类InputController:MonoBehavior {

浮动点击时间;
RayCastHit 2D rayhit;
浮动最小距离=1.5f;
int smallestId=1;
公共转换[]节点;
公共层任务可选择对象层任务;
无效更新()
{
if(Input.GetMouseButtonDown(0))
{
单击时间=Time.Time;
rayhit=Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition)、Vector2.zero、Mathf.Infinity、selectableObjLayerMask);
}
else if(Input.GetMouseButtonUp(0))
{
如果(rayhit)
{
如果(Time.Time-单击时间<.2f)
{
Node Node=rayhit.transform.GetComponent();
如果(节点!=null)
{
对于(int i=0;i180?newAngles.z-180:newAngles.z;
node.sticks[i].transform.localEulerAngles=newAngles;
node.sticks[i].degree=(int)newAngles.z;
Debug.Log(i+“=”+node.sticks[i].degree);
}
}
}
其他的
{
节点currNode=rayhit.transform.GetComponent();
if(currNode.isMoved==false)
{
smallestId=0;
最小距离=999;
for(int i=0;i距离)
{
最小距离=距离;
smallestId=i;
Log(“Obje:+currNode”);
}
}
rayit.transform.position=节点[smallestId].transform.position;
if(rayhit.transform.parent!=节点[smallestId].transform)
{ 
if(节点[smallestId].transform.childCount>0&&nodes[smallestId].transform!=rayhit.transform.parent)
{
if(currNode!=null)
{
对于(int i=0;i=0.2f)
{
Vector2 newPos=Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position=newPos;
}
}
}
}
}

}

你试过什么?有什么问题?我是如何具体地将其与Visual Studio联系起来的。。。一般来说:您应该存储和重用
GetComponent
的结果,而不是在每一帧都重新获取它……我没有尝试过某些东西,因为我无法准确地解决如何获取节点1、2、3和生成的节点的方向。但是我找不到一种方法来精确地比较它们。我想我应该检查两个循环中的每两个节点。但是我不能澄清:\btw我错误地添加了visual studio,我再次删除了它。
using System.Collections;
float clickTime;
RaycastHit2D rayhit;
float smallestDistance = 1.5f;
int smallestId = 1;
public Transform[] nodes;
public LayerMask selectableObjLayerMask;


void Update()
{


    if (Input.GetMouseButtonDown(0))
    {

        clickTime = Time.time;
        rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
    }
    else if (Input.GetMouseButtonUp(0))
    {
        if (rayhit)
        {
            if (Time.time - clickTime < .2f)
            {
                Node node = rayhit.transform.GetComponent<Node>();
                if (node != null)
                {
                    for (int i = 0; i < node.sticks.Count; i++)
                    {
                        Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
                        newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
                        newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
                        node.sticks[i].transform.localEulerAngles = newAngles;

                        node.sticks[i].degree = (int)newAngles.z;
                        Debug.Log(i + " = " + node.sticks[i].degree);


                    }

                }
            }
            else 
            {
                Node currNode = rayhit.transform.GetComponent<Node>();
                if(currNode.isMoved == false)
                {
                    smallestId = 0;
                    smallestDistance = 999;
                    for (int i = 0; i < nodes.Length; i++)
                    {

                        float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
                        if (smallestDistance > distance)
                        {
                            smallestDistance = distance;
                            smallestId = i;

                            Debug.Log("Obje : " + currNode);
                        }
                    }

                    rayhit.transform.position = nodes[smallestId].transform.position;
                    if (rayhit.transform.parent != nodes[smallestId].transform)
                    { 
                        if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
                        {
                            if (currNode != null)
                            {
                                for (int i = 0; i < currNode.sticks.Count; i++)
                                {
                                    nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
                                    currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
                                }
                                Destroy(rayhit.transform.gameObject);
                            }
                        }
                        else
                        {
                            if (currNode != null)
                            {
                                currNode.isMoved = true;
                            }
                            rayhit.transform.SetParent(nodes[smallestId].transform);
                        }
                    }

                }

            }               

        }
        rayhit = new RaycastHit2D();

    }
    else if (Input.GetMouseButton(0))
    {
        if(rayhit.transform != null)
        {
            Node currNode = rayhit.transform.GetComponent<Node>();
            if(currNode != null)
                if (currNode.isMoved == false)
                {
                    if (Time.time - clickTime >= 0.2f)
                    {
                        Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        rayhit.transform.position = newPos;
                    }
                }
        }


    }
}