Unity3d(c#)中的功能无法正常工作

时间:2016-02-14 13:39:34

标签: c# unity3d 2d

我试图在2d内建造一些建筑物,但不应该与其他建筑物接触。
我有一个

private GameObject building;


从更新我称为函数

buildFence(Vector2 nextPos)

我在哪里创建新建筑物 从这个函数我调用函数必须返回布尔类型

public bool canbuild()
{        
    //Debug.Log(building);
    if (building.GetComponent<Rigidbody2D>().IsTouchingLayers(LayerMask.GetMask("Buildings")))
    {
        building.GetComponent<SpriteRenderer>().color = Color.red;
        //Debug.Log("canb=false");           
        return false;
    }
    else {
        building.GetComponent<SpriteRenderer>().color = Color.green;           
        return true;
    }
}

但此函数始终返回true。如果我从Update调用相同的函数, 一切顺利。

此处所有代码

void Update()
{
    if (mode)
    {
        if (TEST)
        {              
            if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
            {
                if (Input.GetMouseButtonDown(0) && idBuilding != 0 && buildFlag)
                {   
                    buildFlag = false;
                    startpos = cam.ScreenToWorldPoint(Input.mousePosition);

                    if (idBuilding != 1)
                    {
                        building = (GameObject)Instantiate(buildings[idBuilding - 1], startpos, new Quaternion(0, 0, 0, 0));
                        building.GetComponent<Collider2D>().isTrigger = true;
                        canbuild();
                    }
                    else
                    {

                    }
                }
                if (Input.GetMouseButton(0) && idBuilding != 0)
                {
                    if (idBuilding != 1)
                    {
                        Vector2 pos = cam.ScreenToWorldPoint(Input.mousePosition);
                        building.transform.position = pos;
                        canbuild();
                    }
                    else
                    {
                        buildFence(cam.ScreenToWorldPoint(Input.mousePosition));
                        if (!canbuild())
                        {
                            Debug.Log("succes");
                        }
                    }
                }
                if (Input.GetMouseButtonUp(0) && idBuilding != 0)
                {
                    if (canbuild())
                    {
                        buildFlag = false;
                        Vector2 viewportPoint = Camera.main.WorldToViewportPoint(building.transform.position + new Vector3(2, 2, 0));
                        bAccept.anchorMin = viewportPoint;
                        bAccept.anchorMax = viewportPoint;
                        bAccept.gameObject.active = true;
                    }
                    else
                    {
                        Debug.Log("CantBuild");
                        Destroy(building);
                        buildFlag = true;
                    }
                    //building.GetComponent<Collider2D>().enabled = true; 
                }
            }
        }
        else
        {

        }
    }
}
void buildFence(Vector2 nextPos)
{
    Vector2 delta = nextPos - startpos;

    //Debug.Log("v2" + (nextPos - startpos));
    //Debug.Log("v2.angle="+ Vector2.Angle(new Vector2(x, y), (nextPos - startpos)));
    //Debug.Log("v2.angle=" +);

    if (delta.magnitude >= 2.55 && canBuildNext)
    {
        if (delta.x < 0)
            building = (GameObject)Instantiate(buildings[0], startpos, Quaternion.Euler(0, 0, Vector2.Angle(new Vector2(x, y), (nextPos - startpos))));
        else
            building = (GameObject)Instantiate(buildings[0], startpos, Quaternion.Euler(0, 0, -Vector2.Angle(new Vector2(x, y), (nextPos - startpos))));

        building.GetComponent<Collider2D>().isTrigger = true;
        canBuildNext = false;
    }
    if (!canBuildNext)
    {
        if (!canbuild(ref building))
        {
            Debug.Log("HERE");

            if (delta.x < 0)
                building.transform.rotation = Quaternion.Euler(0, 0, Vector2.Angle(new Vector2(x, y), (nextPos - startpos)));
            else
                building.transform.rotation = Quaternion.Euler(0, 0, -Vector2.Angle(new Vector2(x, y), (nextPos - startpos)));
        }
        else
        {
            startpos = nextPos;
            //building.GetComponent<Collider2D>().isTrigger = false;
            Zabor.Push(building);
            canBuildNext = true;
        }
    }

}

public bool canbuild()
{
    //Debug.Log(b);
    //Debug.Log(building);
    if (building.GetComponent<Rigidbody2D>().IsTouchingLayers(LayerMask.GetMask("Buildings")))
    {
        building.GetComponent<SpriteRenderer>().color = Color.red;
        //Debug.Log("canb=false");
        buildfail = false;
        return false;
    }
    else {
        building.GetComponent<SpriteRenderer>().color = Color.green;
        buildfail = true;
        return true;
    }
}

0 个答案:

没有答案