网格两侧的对象

时间:2016-12-08 15:26:00

标签: c# unity3d

我使用这个代码放置墙壁,它工作得很好,唯一的问题是,墙(预制高度为4)放在网格的两侧 - 就像一半墙在墙上,另一半在墙下,我该如何解决?

bool creating;
public GameObject start;
public GameObject end;

public GameObject wallPrefab;
GameObject wall;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (!EventSystem.current.IsPointerOverGameObject()) 
    {
        getInput();
    }
}

void getInput()
{
    if (Input.GetMouseButtonDown(0))
    {
        setStart();
    }
    else if (Input.GetMouseButtonUp(0))
    {
        setEnd();
    }
    else
    {
        if (creating)
        {
            adjust();
        }
    }

}


void setStart()
{
    creating = true;
    start.transform.position = getWorldPoint();
    wall = (GameObject)Instantiate(wallPrefab, start.transform.position, Quaternion.identity) as GameObject;
}


void setEnd()
{
    creating = false;
    end.transform.position = getWorldPoint();
}



void adjust()
{
    end.transform.position = getWorldPoint();
    adjustWall();
}

void adjustWall()
{
    start.transform.LookAt(end.transform.position);
    end.transform.LookAt(start.transform.position);
    float distance = Vector3.Distance(start.transform.position, end.transform.position);
    wall.transform.position = start.transform.position + distance / 2 * start.transform.forward;
    wall.transform.rotation = start.transform.rotation;
    wall.transform.localScale = new Vector3(wall.transform.localScale.x, wall.transform.localScale.y, distance);
}


Vector3 getWorldPoint()
{
    Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        return hit.point;
    }
    return Vector3.zero;
}

0 个答案:

没有答案